Solved

SQL Insert and Update Commands in vb.net

Posted on 2012-12-27
11
562 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 2
11 Comments
 
LVL 22

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

734 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