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.

Recommended Answers

All 5 Replies

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.

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?

Have you checked that your dataset contains rows?

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.