Solved

SQL Insert and Update Commands in vb.net

Posted on 2012-12-27
11
551 Views
Last Modified: 2012-12-27
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
Comment
Question by:arozzy
  • 8
  • 2
11 Comments
 
LVL 21

Expert Comment

by:mcsween
ID: 38724434
Try

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

Open in new window

0
 

Author Comment

by:arozzy
ID: 38724452
thanks for reply

but the same error exist again
0
 
LVL 12

Accepted Solution

by:
Jared_S earned 500 total points
ID: 38724537
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
 

Author Comment

by:arozzy
ID: 38724539
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
 

Author Comment

by:arozzy
ID: 38724577
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:arozzy
ID: 38724683
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
 

Author Comment

by:arozzy
ID: 38724799
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
 

Author Closing Comment

by:arozzy
ID: 38724805
Thanks your suggestion  Jared_s
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38724865
That was fast - I'm glad it is working.
0
 

Author Comment

by:arozzy
ID: 38724900
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
 

Author Comment

by:arozzy
ID: 38724907
thank you again Jared_s
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now