We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,657 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sql Server Connection Problem

Hi....
I am trying to connect sql server with vb.net in Visual Studio 2008 with foll. code....
but it is having problem....what is the problem? Please tell me....
1. I created "publication" databse. In table i took 2 columns...fname and lname and fill the records...
2. I took DataGridView onto form and did foll. code....
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form
Dim objConnection As SqlConnection = New SqlConnection("server=(local);database=publication;integrated Security=SSPI;")
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet As DataSet = New DataSet()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "select * from student"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
objConnection.Open()
objDataAdapter.Fill(objDataSet, "student")
objConnection.Close()
grdPublication.DataSource = objDataSet
grdPublication.DataMember = "student"
objDataAdapter = Nothing
objConnection = Nothing

End Sub
End Class


But, it doesn't show any outpou....What to do?


Thanks & Regards,
Pooja.

2
Contributors
5
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
6
Views
deolalkar_pooja
Light Poster
36 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I guess you should put a breakpoint just before the objConnection.close() and start using messagebox or debug.print to verify the number of records in objDataSet.tables("student")
Once you know if you got any results then you'll know where to continue troubleshooting (the connection/db or your datagridview)

PS: Check the value of grdPublication.AutoGenerateColumns it should be true since you are not adding columns manually. If it's set to False then set it to True just before setting the datasource.

adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

I guess you should put a breakpoint just before the objConnection.close() and start using messagebox or debug.print to verify the number of records in objDataSet.tables("student")
Once you know if you got any results then you'll know where to continue troubleshooting (the connection/db or your datagridview)

PS: Check the value of grdPublication.AutoGenerateColumns it should be true since you are not adding columns manually. If it's set to False then set it to True just before setting the datasource.

Yes....grdPublication.AutoGenerateColumns is set to true....but i am not understanding what is the actual problem?

deolalkar_pooja
Light Poster
36 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Have you checked that your dataset contains rows?

adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

@adam_k.....
i am using dataGridView not dataset....i am beginer to all this thinks...i just want to display data from databse....will u please tell me step by step solution?

deolalkar_pooja
Light Poster
36 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This: Dim objDataSet As DataSet = New DataSet() means that you are using a dataset.
A dataset is a collection of datatables or a way to temporarily store data in memory. A datagridview is a way of presenting data to the user.

Since you are not seeing any data in your datagridview this means that either you are not retrieving them correctly or that you are not showing them correctly.

A step by step approach to checking if you've retrieved data:
Below the fill command enter msgbox(objDataSet.tables("student").rows.count) Run your program. You'll get a message box with the number of rows in your dataset for the table student. If it's not 0 then we need to check the datagridview. If it is 0 then it's your db.

adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0676 seconds using 2.66MB