sql INSERT ... Need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pratham2003
    New Member
    • Apr 2009
    • 4

    sql INSERT ... Need help

    Code:
    id             bigint                 Not Null   Auto Increment
    subject     nchar(32)           Not Null
    chapter     smallint             Not Null
    type         smallint             Not Null
    question   nchar(512)         Not Null
    opt1         nchar(128)         Not Null
    opt2         nchar(128)         Not Null
    opt3         nchar(128)         Null
    opt4         nchar(128)         Null
    opt5         nchar(128)         Null
    opt6         nchar(128)         Null


    Code:
    connectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    dbCon = New SqlClient.SqlConnection(connectionString)
    sqlCom.Connection = dbCon
                sqlCom.CommandType = CommandType.Text
                sqlCom.CommandText = "INSERT INTO testDB(subject, chapter, type, question,opt1,opt2,opt3,opt4,opt5,opt6) VALUES(" + "'" + subject + "'" + "," & chapter & "," & type.ToString & "," + "'" + q + "'" + "," + "'" + op1 + "'" + "," + "'" + op2 + "'" + "," + "'" + op3 + "'" + "," + "'" + op4 + "'" + "," + "'" + op5 + "'" + "," + "'" + op6 + "'" + ")"
                Try
                    dbCon.Open()
                    sqlCom.ExecuteNonQuery()
                    dbCon.Close()
                Catch ex As Exception
                End Try
    I am using vb express 2005 and sql express 2005
    Query runs successfully... But the record is not added... I have checked that there is no exception thrown by sql using the debugger....
    dbCon.Open()
    sqlCom.ExecuteN onQuery()
    dbCon.Close()
    Catch ex As Exception
    these three lines execute and ex is not assigned anything...
    Please help
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    Hi, try the following code:

    Code:
    connectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" 
    dbCon = New SqlClient.SqlConnection(connectionString) 
    sqlCom.Connection = dbCon
    sqlCom.CommandType = CommandType.Text 
    sqlCom.InsertCommand = "INSERT INTO testDB(subject, chapter, type, question,opt1,opt2,opt3,opt4,opt5,opt6) VALUES(" + "'" + subject + "'" + "," & chapter & "," & type.ToString & "," + "'" + q + "'" + "," + "'" + op1 + "'" + "," + "'" + op2 + "'" + "," + "'" + op3 + "'" + "," + "'" + op4 + "'" + "," + "'" + op5 + "'" + "," + "'" + op6 + "'" + ")" 
    Try 
      dbCon.Open() 
      sqlCom.InsertCommand.ExecuteNonQuery() 
      dbCon.Close() 
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try
    Steven

    Comment

    Working...