6 Replies - 269 Views - Last Post: 26 June 2013 - 08:58 PM Rate Topic: -----

#1 Kaushik0007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-June 13

invalid column name

Posted 25 June 2013 - 10:10 PM

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim conn As New SqlClient.SqlConnection
            Dim cmd As New SqlClient.SqlCommand
            Dim dr As New SqlClient.SqlDataAdapter
            Dim dt As New DataTable
            conn.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\WSSD.mdf;Integrated Security=True;User Instance=True")
            cmd.CommandText = "INSERT INTO Consumer (Name, Address, Contact, Connectionno, accountno, catagory, ferrulesize, tapes, toiletseat, metermake, metersize, meterno)" & _
               "VALUES (" & NameTextBox.Text & "," & HousenoTextBox.Text & "," & ContactTextBox.Text & "," & W_S_connectionTextBox.Text & "," & AccountTextBox.Text & "," & CatagoryComboBox.Text & "," & FerrulsizeTextBox.Text & "," & TapesTextBox.Text & "," & ToiletseatTextBox.Text & "," & MetermakeTextBox.Text & "," & MetersizeTextBox.Text & "," & MeternoTextBox.Text & ")"
            cmd.Connection = conn
            conn.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        'NameTextBox.Text = ""
        'HousenoTextBox.Text = ""
        'ContactTextBox.Text = ""
        'W_S_connectionTextBox.Text = ""
        'AccountTextBox.Text = ""
        'CatagoryComboBox.SelectedText = ""
        'FerrulsizeTextBox.Text = ""
        'TapesTextBox.Text = ""
        'ToiletseatTextBox.Text = ""
        'MetermakeTextBox.Text = ""
        'MetersizeTextBox.Text = ""
        'MeternoTextBox.Text = ""

    End Sub



and error as shown in image

Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: invalid column name

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9048
  • View blog
  • Posts: 33,971
  • Joined: 12-June 08

Re: invalid column name

Posted 25 June 2013 - 10:13 PM

So.. SQL typically recognizes a string as being characters between two double quotes.

"abc123" -> this is a string
abc123 -> this is thought of as a column.

What you should be doing is using parameters, but if not then just flank your strings with quotes.
Was This Post Helpful? 0
  • +
  • -

#3 Kaushik0007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-June 13

Re: invalid column name

Posted 25 June 2013 - 10:19 PM

i don't understand what u are trying to understand me
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9048
  • View blog
  • Posts: 33,971
  • Joined: 12-June 08

Re: invalid column name

Posted 25 June 2013 - 10:23 PM

The underlined need to have quotes around them so the string the string is formed right and SQL doesn't think your data you entered is a column name.

Quote

"VALUES (" & NameTextBox.Text & "," & HousenoTextBox.Text & ","

Was This Post Helpful? 0
  • +
  • -

#5 vks.gautam1  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 317
  • Joined: 21-March 08

Re: invalid column name

Posted 25 June 2013 - 10:49 PM

use parameters for this..that is the best way to do things .
this will help you, after this if you find any problem you can ask here
http://social.msdn.m...base-with-vbnet

This post has been edited by vks.gautam1: 25 June 2013 - 10:52 PM

Was This Post Helpful? 1
  • +
  • -

#6 x-logan  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 18-April 13

Re: invalid column name

Posted 26 June 2013 - 04:17 AM

What Modi 123_1 try to convey you is that if your field is character field (say your name field is character)
write it as (use ' ')

'" & NameTextBox.Text & "'


Else if your field is numeric(say contactno field is numeric)
then write

" & ContactTextBox.Text & "

This post has been edited by x-logan: 26 June 2013 - 04:18 AM

Was This Post Helpful? 1
  • +
  • -

#7 Kaushik0007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-June 13

Re: invalid column name

Posted 26 June 2013 - 08:58 PM

a big thnx gys thnx very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1