Link to home
Start Free TrialLog in
Avatar of arozzy
arozzy

asked on

SQL Insert and Update Commands in vb.net

Hi Experts,

I have some trouble with sql commands in VB.net,  

On a winform, I am trying to insert new data to Ms sql database table, each data has its own text.box areas.

my codes,

    Dim adptr2 As New SqlDataAdapter("select * from  STOKANA", baglan)
    Dim stkekle As New SqlCommand("insert into STOKANA values(@SN,@SA,@SM,@SB,@EM,@RD)", baglan)
    Dim ds1 As New DataSet


button click event
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Then
            MessageBox.Show("Stok adi giriniz.", "STOK ADI", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            TextBox1.Focus()
        ElseIf TextBox2.Text = "" Then
            MessageBox.Show("Stok miktarini giriniz.", "STOK MIKTARI", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            TextBox2.Focus()
        ElseIf ComboBox1.Text = "" Then
            MessageBox.Show("Stok birimini giriniz.", "STOK BIRIMI", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            TextBox2.Focus()
        ElseIf TextBox4.Text = "" Then
            MessageBox.Show("Stok miktarini giriniz.", "STOK MIKTARI", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            TextBox4.Focus()
        Else

            Try
                stkekle.Parameters.AddWithValue("SN", TextBox10.Text)
                stkekle.Parameters.AddWithValue("SA", TextBox1.Text)
                stkekle.Parameters.AddWithValue("SM", TextBox2.Text)
                stkekle.Parameters.AddWithValue("SB", ComboBox1.Text)
                stkekle.Parameters.AddWithValue("EM", TextBox4.Text)
                stkekle.Parameters.AddWithValue("RD", CheckBox1.CheckState)
                baglan.Open()
                adptr2.InsertCommand = stkekle
                stkekle.ExecuteNonQuery()
                MsgBox("Kayit eklendi !!")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            baglan.Close()
            Call veri1()
            End If
    End Sub

after adding first data row on sql table, I could not add second data row  and the error massage is that "sql data sqlcient sql expection (0x80131904). The variable name '@SN' has already been declared.  


The update command

I am using datagrid to show user which data he wanna update and data is going to on  textboxes and checkboxes on the form by clicking on selected datagird row object.
when updating the data,  the error massage is that sql data sqlcient sql expection (0x80131904). Error converting data type nvarchar to numeric.

Please help

Thank you

Oguzhan
Avatar of Bradley Fox
Bradley Fox
Flag of United States of America image

Try

insert into STOKANA (Field1,Field2,Field3,Field4,Field5,Field6) VALUES(@SN,@SA,@SM,@SB,@EM,@RD)

Open in new window

Avatar of arozzy
arozzy

ASKER

thanks for reply

but the same error exist again
ASKER CERTIFIED SOLUTION
Avatar of Jared_S
Jared_S

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arozzy

ASKER

insert command works like that, I changed the place of stkekle command under try, by doing this help program  having new parameter after each add click
 
TRY            
Dim stkekle As New SqlCommand("insert into STOKANA values(@SN,@SA,@SM,@SB,@EM,@RD)", baglan)
                stkekle.Parameters.AddWithValue("SN", TextBox10.Text)
                stkekle.Parameters.AddWithValue("SA", TextBox1.Text)
                stkekle.Parameters.AddWithValue("SM", TextBox2.Text)
                stkekle.Parameters.AddWithValue("SB", ComboBox1.Text)
                stkekle.Parameters.AddWithValue("EM", TextBox4.Text)
                stkekle.Parameters.AddWithValue("RD", CheckBox1.CheckState)
Avatar of arozzy

ASKER

it's my update command for datagrid textboxes and a  checkbox . With this codes, I want to do get selected datagrid row's value for textboxes and checkbox. After getting values on  boxes, I want to update data row again but as jared_s says the converting data is probem.

How can I convert textbox and checkbox values by getting from datagrid to boxes into the sql table ?

Thanks
 
 Private Sub DataGridView4_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellContentClick
        TextBox8.Text = DataGridView4.CurrentRow.Cells(1).Value
        TextBox7.Text = DataGridView4.CurrentRow.Cells(2).Value
        ComboBox2.Text = DataGridView4.CurrentRow.Cells(3).Value
        TextBox6.Text = DataGridView4.CurrentRow.Cells(4).Value
        CheckBox2.CheckState = DataGridView4.CurrentRow.Cells(5).Value
    End Sub

 Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If DataGridView4.CurrentRow.Cells(0).Value.ToString() = "" Then
            MessageBox.Show("Lütfen Siinecek Olan Stok'u Seçiniz.", "STOK NO", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            DataGridView3.Focus()
        Else
            Try
                Dim stkdzlt As New SqlCommand("update STOKANA set STOKAD = @SA2, STOKMIK = @SM2, STOKBRM = @SB2, EMNMIK = @EM2, ALTRCT = @RD2", baglan)
                stkdzlt.Parameters.AddWithValue("@SA2", TextBox8.Text)
                stkdzlt.Parameters.AddWithValue("@SM2", TextBox7.Text)
                stkdzlt.Parameters.AddWithValue("@SB2", ComboBox2.Text)
                stkdzlt.Parameters.AddWithValue("@EM2", TextBox6.Text)
                stkdzlt.Parameters.AddWithValue("@RD2", CheckBox2.CheckState)
                baglan.Open()
                adptr2.UpdateCommand = stkdzlt
                stkdzlt.ExecuteNonQuery()
                MsgBox("Kayit düzeltildi !!")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            baglan.Close()
            Call veri2()
            Call veri1()
            Call veri3()
        End If
    End Sub
Avatar of arozzy

ASKER

I tried to do this but this time error code incudes that " incorrect syntax near the keyword 'conver'  

 Try
                Dim stkdzlt As New SqlCommand("update STOKANA set STOKAD = @SA2(CONVERT(varchar(200)), STOKMIK = @SM2(CONVERT(decmial(18,3)), STOKBRM = @SB2(CONVERT(varchar(200)), EMNMIK = @EM2(CONVERT(decmial(18,3)), ALTRCT = @RD2(CONVERT(char(1))", baglan)
                stkdzlt.Parameters.AddWithValue("@SA2", TextBox8.Text.ToString())
                stkdzlt.Parameters.AddWithValue("@SM2", TextBox7.Text)
                stkdzlt.Parameters.AddWithValue("@SB2", ComboBox2.Text.ToString())
                stkdzlt.Parameters.AddWithValue("@EM2", TextBox6.Text)
                stkdzlt.Parameters.AddWithValue("@RD2", CheckBox2.CheckState)
Avatar of arozzy

ASKER

I found this way to solve syntax error, updating and inserting are working exellent. Thanks for your suggestion

Private Sub DataGridView4_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellContentClick
        TextBox8.Text = DataGridView4.CurrentRow.Cells(1).Value
        TextBox7.Text = DataGridView4.CurrentRow.Cells(2).Value.ToString
        ComboBox2.Text = DataGridView4.CurrentRow.Cells(3).Value
        TextBox6.Text = DataGridView4.CurrentRow.Cells(4).Value.ToString
        CheckBox2.CheckState = DataGridView4.CurrentRow.Cells(5).Value.ToString
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If DataGridView4.CurrentRow.Cells(0).Value.ToString() = "" Then
            MessageBox.Show("Lütfen Siinecek Olan Stok'u Seçiniz.", "STOK NO", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            DataGridView3.Focus()
        Else
            Try
                              Dim stkdzlt As New SqlCommand("update STOKANA set STOKAD = @SA2, STOKMIK = @SM2, STOKBRM = @SB2, EMNMIK = @EM2, ALTRCT = @RD2 where STOKID = @SN2", baglan)
                stkdzlt.Parameters.AddWithValue("@SN2", CInt(DataGridView4.CurrentRow.Cells(0).Value))
                stkdzlt.Parameters.AddWithValue("@SA2", TextBox8.Text.ToString())
                stkdzlt.Parameters.AddWithValue("@SM2", CDec(TextBox7.Text))
                stkdzlt.Parameters.AddWithValue("@SB2", ComboBox2.Text.ToString())
                stkdzlt.Parameters.AddWithValue("@EM2", CDec(TextBox6.Text))
                stkdzlt.Parameters.AddWithValue("@RD2", CheckBox2.CheckState)
                baglan.Open()
                adptr2.UpdateCommand = stkdzlt
                stkdzlt.ExecuteNonQuery()
                MsgBox("Kayit düzeltildi !!")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            baglan.Close()
Avatar of arozzy

ASKER

Thanks your suggestion  Jared_s
That was fast - I'm glad it is working.
Avatar of arozzy

ASKER

This query is for MS sql, Isnt it ?

is it obligatory for disable conflict or that way I am using does not allow any error ?
Avatar of arozzy

ASKER

thank you again Jared_s