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.AddWith Value("SN" , TextBox10.Text)
stkekle.Parameters.AddWith Value("SA" , TextBox1.Text)
stkekle.Parameters.AddWith Value("SM" , TextBox2.Text)
stkekle.Parameters.AddWith Value("SB" , ComboBox1.Text)
stkekle.Parameters.AddWith Value("EM" , TextBox4.Text)
stkekle.Parameters.AddWith Value("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
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
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.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
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
ASKER
thanks for reply
but the same error exist again
but the same error exist again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.AddWith Value("SN" , TextBox10.Text)
stkekle.Parameters.AddWith Value("SA" , TextBox1.Text)
stkekle.Parameters.AddWith Value("SM" , TextBox2.Text)
stkekle.Parameters.AddWith Value("SB" , ComboBox1.Text)
stkekle.Parameters.AddWith Value("EM" , TextBox4.Text)
stkekle.Parameters.AddWith Value("RD" , CheckBox1.CheckState)
TRY
Dim stkekle As New SqlCommand("insert into STOKANA values(@SN,@SA,@SM,@SB,@EM
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
stkekle.Parameters.AddWith
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_CellContentC lick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles DataGridView4.CellContentC lick
TextBox8.Text = DataGridView4.CurrentRow.C ells(1).Va lue
TextBox7.Text = DataGridView4.CurrentRow.C ells(2).Va lue
ComboBox2.Text = DataGridView4.CurrentRow.C ells(3).Va lue
TextBox6.Text = DataGridView4.CurrentRow.C ells(4).Va lue
CheckBox2.CheckState = DataGridView4.CurrentRow.C ells(5).Va lue
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If DataGridView4.CurrentRow.C ells(0).Va lue.ToStri ng() = "" 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.AddWith Value("@SA 2", TextBox8.Text)
stkdzlt.Parameters.AddWith Value("@SM 2", TextBox7.Text)
stkdzlt.Parameters.AddWith Value("@SB 2", ComboBox2.Text)
stkdzlt.Parameters.AddWith Value("@EM 2", TextBox6.Text)
stkdzlt.Parameters.AddWith Value("@RD 2", 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
How can I convert textbox and checkbox values by getting from datagrid to boxes into the sql table ?
Thanks
Private Sub DataGridView4_CellContentC
TextBox8.Text = DataGridView4.CurrentRow.C
TextBox7.Text = DataGridView4.CurrentRow.C
ComboBox2.Text = DataGridView4.CurrentRow.C
TextBox6.Text = DataGridView4.CurrentRow.C
CheckBox2.CheckState = DataGridView4.CurrentRow.C
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If DataGridView4.CurrentRow.C
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.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
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
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.AddWith Value("@SA 2", TextBox8.Text.ToString())
stkdzlt.Parameters.AddWith Value("@SM 2", TextBox7.Text)
stkdzlt.Parameters.AddWith Value("@SB 2", ComboBox2.Text.ToString())
stkdzlt.Parameters.AddWith Value("@EM 2", TextBox6.Text)
stkdzlt.Parameters.AddWith Value("@RD 2", CheckBox2.CheckState)
Try
Dim stkdzlt As New SqlCommand("update STOKANA set STOKAD = @SA2(CONVERT(varchar(200))
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
ASKER
I found this way to solve syntax error, updating and inserting are working exellent. Thanks for your suggestion
Private Sub DataGridView4_CellContentC lick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles DataGridView4.CellContentC lick
TextBox8.Text = DataGridView4.CurrentRow.C ells(1).Va lue
TextBox7.Text = DataGridView4.CurrentRow.C ells(2).Va lue.ToStri ng
ComboBox2.Text = DataGridView4.CurrentRow.C ells(3).Va lue
TextBox6.Text = DataGridView4.CurrentRow.C ells(4).Va lue.ToStri ng
CheckBox2.CheckState = DataGridView4.CurrentRow.C ells(5).Va lue.ToStri ng
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If DataGridView4.CurrentRow.C ells(0).Va lue.ToStri ng() = "" 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.AddWith Value("@SN 2", CInt(DataGridView4.Current Row.Cells( 0).Value))
stkdzlt.Parameters.AddWith Value("@SA 2", TextBox8.Text.ToString())
stkdzlt.Parameters.AddWith Value("@SM 2", CDec(TextBox7.Text))
stkdzlt.Parameters.AddWith Value("@SB 2", ComboBox2.Text.ToString())
stkdzlt.Parameters.AddWith Value("@EM 2", CDec(TextBox6.Text))
stkdzlt.Parameters.AddWith Value("@RD 2", CheckBox2.CheckState)
baglan.Open()
adptr2.UpdateCommand = stkdzlt
stkdzlt.ExecuteNonQuery()
MsgBox("Kayit düzeltildi !!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
baglan.Close()
Private Sub DataGridView4_CellContentC
TextBox8.Text = DataGridView4.CurrentRow.C
TextBox7.Text = DataGridView4.CurrentRow.C
ComboBox2.Text = DataGridView4.CurrentRow.C
TextBox6.Text = DataGridView4.CurrentRow.C
CheckBox2.CheckState = DataGridView4.CurrentRow.C
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If DataGridView4.CurrentRow.C
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.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
stkdzlt.Parameters.AddWith
baglan.Open()
adptr2.UpdateCommand = stkdzlt
stkdzlt.ExecuteNonQuery()
MsgBox("Kayit düzeltildi !!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
baglan.Close()
ASKER
Thanks your suggestion Jared_s
That was fast - I'm glad it is working.
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 ?
is it obligatory for disable conflict or that way I am using does not allow any error ?
ASKER
thank you again Jared_s
Open in new window