Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Connection string error VS2010

Author  Topic 

mulefeathers
Starting Member

12 Posts

Posted - 2012-11-09 : 11:44:15
Using the data source wizard to create a data connection to a SQL database in VS2010. The following code was created:

Me.TblPartsTableAdapter.Fill(Me.KanbanDataSet.tblParts)

I assume ( probably wrongly) that since the text boxes and combobox that are bound to this database contain the correct data that a connection has been established and the dataset filled. So I want to query this data with a selected change event. I declare global variables

Dim strprocess As String
Dim strpart As String = Nothing

Then under the selected change event I use:

strprocess = cboprocess.SelectedValue

strpart = "SELECT PartNumber FROM tblParts WHERE Process = @Process"
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(strpart)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Process", strprocess)

Dim someDatatable As New DataTable
Dim myReader As SqlClient.SqlDataReader = cmd.ExecuteReader
If myReader.HasRows Then
someDatatable.Load(myReader)
End If


The above code should take the selectedvalue of cboprocess put in the query and return a value to strpart. Then I will bind the results to cbopartnmber. When running the code I receive this error:

ExecuteReader: Connection property has not been initialized.

I think generally this means the connection is not open but itf its not open how is there data in the dataset. Is the datasource wizard a big POS?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-09 : 11:56:15
You need a connection object attached to the command object. See example on this page:
http://msdn.microsoft.com/en-us/library/dw70f090.aspx?cs-save-lang=1&cs-lang=vb#_SqlClient
Go to Top of Page
   

- Advertisement -