1,105,556 Community Members

Sql Server Connection Problem

Member Avatar
deolalkar_pooja
Light Poster
38 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
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.

Member Avatar
deolalkar_pooja
Light Poster
38 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

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

Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

Have you checked that your dataset contains rows?

Member Avatar
deolalkar_pooja
Light Poster
38 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@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?

Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: