Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

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
0
arozzy
Asked:
arozzy
  • 8
  • 2
1 Solution
 
mcsweenSr. Network AdministratorCommented:
Try

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

Open in new window

0
 
arozzyAuthor Commented:
thanks for reply

but the same error exist again
0
 
Jared_SCommented:
What happens if you move the declaration of your sql command into your TRY statement? Does it fix your variable declaration issue?

The update issue seems like you're trying to update a numeric only column in your table with a non-numeric value. You should convert the value prior before you perform your update.
Just look at the table's schema to see which columns are numeric and then go from there.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
arozzyAuthor Commented:
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)
0
 
arozzyAuthor Commented:
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
0
 
arozzyAuthor Commented:
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)
0
 
arozzyAuthor Commented:
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()
0
 
arozzyAuthor Commented:
Thanks your suggestion  Jared_s
0
 
Jared_SCommented:
That was fast - I'm glad it is working.
0
 
arozzyAuthor Commented:
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 ?
0
 
arozzyAuthor Commented:
thank you again Jared_s
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now