datagridview not populating from stored procedure

T

TG

Hi!

I am using VB 2008 with SQL Server 2000 and SQL Server 2005 (depending
which server the user selects to connect to).

I have a listbox in which the user select the server to connect to.

Another listbox that shows the databases in that particular server.

Another listbox with the filesets from the selected database above.

When the user clicks on button 3, I want the datagridview to populate
data from a stored procedure passing 2 parameters in sql server.

At the moment the datagridview does not populate anything, neither
does it throw an error message. I have no clue what is wrong and I am
new to VB.

Your help will be greatly appreciated.

Thanks!

Tammy


Code for button 3:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim cn As New SqlConnection("Data Source=" &
lstServers.SelectedValue & ";Initial Catalog=" &
lstDatabases.SelectedValue & ";Integrated Security=SSPI")

Dim cmd As New SqlCommand("usp_DR_Spam_BB_Search_get_recs",
cn)

Dim dt As New DataTable()

cmd.CommandTimeout = 0
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.AddWithValue("@Matter",
lstDatabases.SelectedItem)
cmd.Parameters.AddWithValue("@FileSet",
lstFileSets.SelectedItem)

cn.Open()


Dim reader As SqlDataReader = cmd.ExecuteReader()


DataGridView1.DataSource = reader


End Sub
 
R

Rich P

Greetings,

Try something like this:

Dim da As New SqlDataAdapter, ds As New Dataset
Dim conn As New SqlConnection

conn.ConnectionString = "Data Source=yourServer;Initial
Catalog=yourDB;Integrated Security=True"

da.SelectCommand = New SqlCommand
da.SelectComnmand.Connection = conn
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.CommandText = "store procedure name"
da.Fill(ds, "tbl1")
Datagridview1.DataSource = ds.Tables("tbl1")

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top