8 Replies - 3727 Views - Last Post: 03 October 2011 - 07:55 AM Rate Topic: ***** 1 Votes

#1 timmack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 31-January 11

What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 02:50 AM

Good Day!

I'm just using the default SQL Server Express in my vb 2008 express edition as my database server for my database operation. When I tried to update the records on my table I got a runtime error which indicates the error on this line 'myCommand.ExecuteNonQuery()' and it highlights a yellow background on it. The runtime error says "SqlException was unhandled: An expression of non-boolean type specified in a context where a condition is expected, near 'Number'. ". What's wrong with my code below? Am I using the correct updating code or am I using the correct where clause? Can you also give me the proper deleting code as well? Please correct it by any chance. Thanks.

*******code********

Imports System.Data.SqlClient

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

Dim myConnection As New SqlConnection
Dim myCommand As New SqlCommand
Dim connect As String

connect = "Data Source =.\SQLExpress; Integrated Security =true; AttachDbFilename =|DataDirectory|\windblow.mdf;User Instance =true;"

myConnection = New SqlConnection(connect)
myConnection.Open()
myCommand = myConnection.CreateCommand
myCommand.CommandText = "UPDATE Table1 SET Name ='" & (TextBox2.Text) & "', Age= '" & (TextBox3.Text) & "', Status='" & (TextBox4.Text) & "' WHERE ID Number ='" & (TextBox1.Text) & "' "

myCommand.ExecuteNonQuery()
myConnection.Close()

MsgBox("New records are edited.", MsgBoxStyle.Information)

End Sub

Is This A Good Question/Topic? 0
  • +

Replies To: What is the proper Update, and Delete code for SQL Express in VB 2008

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 02:56 AM

The problem is that you have statement "WHERE ID Number =", the correct syntax for WHERE should be "WHERE COLUMNNAME =". So what is the name of the column there ID or Number? or ID_Number because space is not allowed in identifiers

EDIT: Also use :code:

This post has been edited by smohd: 03 October 2011 - 02:57 AM

Was This Post Helpful? 1
  • +
  • -

#3 timmack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 31-January 11

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 03:06 AM

Thanks for your reply Smohd! So basically when you make a table in your database, the column name should have no space because I put a space on my ID Number column. So my code is correct except for the column name? Let me check on this. I have to change the column name on my table.
Was This Post Helpful? 0
  • +
  • -

#4 timmack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 31-January 11

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 03:29 AM

Hey Smohd!

Thank you very much!You been a big help. I never knew how simple it was but never realized that rule in the first place. I got it now. Thank you so much friend! You've been so informative.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 03:37 AM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

#6 timmack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 31-January 11

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 06:28 AM

Hey Smohd!

One more thing! What is the proper sql statement if I'd like to query and display the records from my database into the textboxes? What code do I add to this code below in order to display the records into the textboxes?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myConnection As New SqlConnection
        Dim myCommand As New SqlCommand
        Dim connect As String

        connect = "Data Source =.\SQLExpress; Integrated Security =true; AttachDbFilename =|DataDirectory|\windblow.mdf;User Instance =true;"

        myConnection = New SqlConnection(connect)
        myConnection.Open()
        myCommand = myConnection.CreateCommand
        myCommand.CommandText = "SELECT * FROM Table1 WHERE ID_Number ='" & (TextBox1.Text) & "'"

        myCommand.ExecuteReader()
        myConnection.Close()

    End Sub

Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 06:44 AM

First I dont think it text box is a good solution here, you should think of data grid view, list views and others.
To answer the question, you need a data reader there and you are ready to go:
Dim dr As SqlDataReader
dr = myCommand.ExecuteReader() ' this at line 13
'then
While dr.Read()
  textBox1.Text += dr.Item("Column name here") & VbNewLine' so you can get any column according to your need
End While 'and so

Was This Post Helpful? 0
  • +
  • -

#8 timmack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 31-January 11

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 06:58 AM

Hello Smohd!

You know what? I have learned a lot from you today and I greatly appreciated your help. You are very
efficient. Thank you so much again men! You did an excellent job...
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: What is the proper Update, and Delete code for SQL Express in VB 2008

Posted 03 October 2011 - 07:55 AM

Glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1