Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I created a local Sql Database and simple web page w/2 textboxes and a submit button to check first and last name to the data in the database. When I debug it, I get the error at the bottom and REALLY need some help figuring this out. Thank you for your time in advance.

Oh and this was done in Visual Studion 2013.


VB
If TextBox1.Text = "" Or TextBox2.Text = "" Then
          Label1.Text = "Enter name and Last Name"
      Else
          Dim myconn As SqlConnection
          Dim mycomm As SqlCommand
          Dim myadapt As SqlDataAdapter
          myconn = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DBWorkTemp.mdf;Integrated Security=True")
          myconn.Open()
          mycomm = New SqlCommand("SELECT [FirstName], [LastName] FROM [Table] WHERE (([FirstName] = @FirstName) AND ([LastName] = @LastName))")
          mycomm.Parameters.Add(New SqlParameter("@Firstname", SqlDbType.NChar, 10))
          mycomm.Parameters.Add(New SqlParameter("@Lastname", SqlDbType.NChar, 10))
          mycomm.Parameters.AddWithValue("@Firstname", TextBox1.Text)
          mycomm.Parameters.AddWithValue("@Lastname", TextBox2.Text)
          myadapt = New SqlDataAdapter(mycomm.ToString, myconn)
          mycomm.ExecuteNonQuery()
          myconn.Close()
      End If

An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=ExecuteNonQuery: Connection property has not been initialized.
  Source=System.Data
  StackTrace:
       at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at WebApplication1.Login.Button1_Click(Object sender, EventArgs e) in c:\users\dsc\documents\visual studio 2013\Projects\WebApplication1\WebApplication1\Login.aspx.vb:line 35
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
Posted
Updated 4-Sep-14 17:39pm
v2

It seems that you have defined the parameters twice for the query. First in
VB
mycomm.Parameters.Add(New SqlParameter("@Firstname", SqlDbType.NChar, 10))
mycomm.Parameters.Add(New SqlParameter("@Lastname", SqlDbType.NChar, 10))

and then again in
VB
mycomm.Parameters.AddWithValue("@Firstname", TextBox1.Text)
mycomm.Parameters.AddWithValue("@Lastname", TextBox2.Text)

Either define the value for the parameters using the Value property if you use the first ones or leave just the latter ones (probably easier in this case).

Taken from SqlParameterCollection.AddWithValue method[^]
Use AddWithValue whenever you want to add a parameter by specifying its name and value
 
Share this answer
 
v2
So I rem'd out the first two parameter.add lines and still got this error on the mycomm.executenonquery() line :(





An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code

Additional information: ExecuteNonQuery: Connection property has not been initialized.


I have teamviewer if anyone would be willing to log in and help as well. Thank you
 
Share this answer
 
I also tried the other way listed on the link you gave me;


VB
mycomm.Parameters.Add(New SqlParameter("@Firstname", SqlDbType.NChar, 10))
          mycomm.Parameters.Add(New SqlParameter("@Lastname", SqlDbType.NChar, 10))
          mycomm.Parameters("@Firstname").Value = TextBox1.Text
          mycomm.Parameters("@Lastname").Value = TextBox2.Text


          ' mycomm.Parameters.AddWithValue("@Firstname", TextBox1.Text)
          '  mycomm.Parameters.AddWithValue("@Lastname", TextBox2.Text)
          myadapt = New SqlDataAdapter(mycomm.ToString, myconn)
          mycomm.ExecuteNonQuery()


Still having the same error.
 
Share this answer
 
Do you see the difference between
VB
Dim myconn As SqlConnection
Dim mycomm As SqlCommand
?
mycomm has no knowledge of myconn. Consequently, the connection property of mycomm has never been initialized.
mycomm.Connection = myconn
That's the decisive line of code missing.
And what does that myadapt do? Get rid of it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900