0

I have a Table name as sales and another is balance. I want to use insert command in sales and update for the balance table. What can I do?

     cmd.CommandText = " INSERT INTO SALES VALUES('" & ComboBox1.Text & " ' , " & SILVER & " ," & GOLD & ",'" & ComboBox2.Text & "'," & KILO.Text & ", " & TOUCH.Text & " ," & TOTKILO.Text & "," & TextBox3.Text & "," & TextBox8.Text & "," & KGOLD & "," & KSILVER & "," & TextBox9.Text & " ," & TextBox10.Text & "," & TextBox11.Text & "," & TextBox12.Text & " , " & TextBox13.Text & " )"
     Dim NB As Double
    NB = TextBox11.Text
    ST = ComboBox1.SelectedValue.ToString
    cmd.CommandType = " UPDATE BALANCE SET OBBALANCE = " & " " & NB & " " & " WHERE         CUSTOMERNAME =  " & " '" & ST & "'" & " "
    cmd.Connection = con
    cmd.ExecuteNonQuery()
    con.Close()

It shows an error as Conversion from string " UPDATE BALANCE SET OBBALANCE = " to type 'Integer' is not valid.

bobs
  • 21,346
  • 12
  • 63
  • 76
MUKESH
  • 69
  • 1
  • 7
  • 18

3 Answers3

3

You could add a semi colon at the end of the INSERT statement to split them into two separate statements. But your error is that your are setting your CommandType to a string and this is an enumeration: CommandType Enumeration

Community
  • 1
  • 1
Matt Wilko
  • 26,449
  • 10
  • 89
  • 138
1

CommandType is an enum which tells what a value in CommandText is: a query, a table name etc.

You can't put the query text there.

Instead, you should do either of the following (from best to worst):

  1. Put both commands into a stored proc on server side and call the stored proc. This would be a better way.

  2. Append the UPDATE command to the INSERT command.

  3. Create another instance of ADO.Command and run it again in the same transaction.

Quassnoi
  • 398,504
  • 89
  • 603
  • 604
0

You can send both to the server using ;:

cmd.CommandText="insert into ...(...) values(...); update ... set ..."
cmd.Connection=con
cmd.ExecuteNonQuery()

This is also how you'd get back the identity value used, you'd append a ;select @@identity after the insert.

The others covered why CommandType is the wrong property to touch so I'll leave it at that.

Blindy
  • 60,429
  • 9
  • 84
  • 123