How to insert multiple rows from datagridview to SQL database in VB.net?
I have made a Form named form1.vb
.
I have 4 TextBoxes named supid
, supname
, supmobile
, suploc
.
I have a DataGridView named datagridview1
.
I am transferring these 4 TextBoxes data in the DataGridView but when I try to send a DataGridView row in SQL database, it sends only 1 row.
I want multiple rows inserted in SQL server at once when I click the insert button.
There are many tutorials about C language but no tutorial about VB.Net
.
Please help how to do this in VB.Net
.
My code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Else
connection.Open()
Dim thequery As String = "select*from supplierdata where supmobile = @supmobile"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, connection)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Else
reader.Close()
command = New SqlCommand("INSERT INTO supplierdata VALUES (@supid,@supname,@supmobile,@suploc)", connection)
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
command.Parameters.Add("supid", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).value
command.Parameters.Add("supname", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters.Add("supmobile", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters.Add("suploc", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(3).Value
i = command.ExecuteNonQuery()
Next
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
connection.Close()
command.Dispose()
End If
End If
End Sub
sql-server vb.net datagridview
add a comment |
I have made a Form named form1.vb
.
I have 4 TextBoxes named supid
, supname
, supmobile
, suploc
.
I have a DataGridView named datagridview1
.
I am transferring these 4 TextBoxes data in the DataGridView but when I try to send a DataGridView row in SQL database, it sends only 1 row.
I want multiple rows inserted in SQL server at once when I click the insert button.
There are many tutorials about C language but no tutorial about VB.Net
.
Please help how to do this in VB.Net
.
My code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Else
connection.Open()
Dim thequery As String = "select*from supplierdata where supmobile = @supmobile"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, connection)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Else
reader.Close()
command = New SqlCommand("INSERT INTO supplierdata VALUES (@supid,@supname,@supmobile,@suploc)", connection)
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
command.Parameters.Add("supid", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).value
command.Parameters.Add("supname", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters.Add("supmobile", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters.Add("suploc", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(3).Value
i = command.ExecuteNonQuery()
Next
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
connection.Close()
command.Dispose()
End If
End If
End Sub
sql-server vb.net datagridview
I'm fairly certain that I answered this question elsewhere and you have clearly ignored everything I said there. How can I not assume that you would ignore advice provided here too?
– jmcilhinney
Nov 17 '18 at 4:12
hello sir i am new here and i have posted first time here. if you can then pls guide.
– pankaj babbar
Nov 17 '18 at 4:25
As I said, the answer was provided elsewhere. I will say here what I said there. You should should be using a data adapter and aDataTable
. CallFill
on the data adapter to populate theDataTable
and bind it to the grid. Saving the changes is then a matter of callingUpdate
on the data adapter. Of course, you'll need to configure it first, so you should read up on how to do that. I provided this link last time where you could learn how to do that.
– jmcilhinney
Nov 17 '18 at 5:47
@Jimi I am glad you got rid of the field name stupid. :-)
– Mary
Nov 17 '18 at 23:29
add a comment |
I have made a Form named form1.vb
.
I have 4 TextBoxes named supid
, supname
, supmobile
, suploc
.
I have a DataGridView named datagridview1
.
I am transferring these 4 TextBoxes data in the DataGridView but when I try to send a DataGridView row in SQL database, it sends only 1 row.
I want multiple rows inserted in SQL server at once when I click the insert button.
There are many tutorials about C language but no tutorial about VB.Net
.
Please help how to do this in VB.Net
.
My code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Else
connection.Open()
Dim thequery As String = "select*from supplierdata where supmobile = @supmobile"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, connection)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Else
reader.Close()
command = New SqlCommand("INSERT INTO supplierdata VALUES (@supid,@supname,@supmobile,@suploc)", connection)
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
command.Parameters.Add("supid", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).value
command.Parameters.Add("supname", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters.Add("supmobile", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters.Add("suploc", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(3).Value
i = command.ExecuteNonQuery()
Next
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
connection.Close()
command.Dispose()
End If
End If
End Sub
sql-server vb.net datagridview
I have made a Form named form1.vb
.
I have 4 TextBoxes named supid
, supname
, supmobile
, suploc
.
I have a DataGridView named datagridview1
.
I am transferring these 4 TextBoxes data in the DataGridView but when I try to send a DataGridView row in SQL database, it sends only 1 row.
I want multiple rows inserted in SQL server at once when I click the insert button.
There are many tutorials about C language but no tutorial about VB.Net
.
Please help how to do this in VB.Net
.
My code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Else
connection.Open()
Dim thequery As String = "select*from supplierdata where supmobile = @supmobile"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, connection)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Else
reader.Close()
command = New SqlCommand("INSERT INTO supplierdata VALUES (@supid,@supname,@supmobile,@suploc)", connection)
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
command.Parameters.Add("supid", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).value
command.Parameters.Add("supname", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters.Add("supmobile", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters.Add("suploc", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(3).Value
i = command.ExecuteNonQuery()
Next
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
connection.Close()
command.Dispose()
End If
End If
End Sub
sql-server vb.net datagridview
sql-server vb.net datagridview
edited Nov 17 '18 at 11:29
Jimi
8,12241934
8,12241934
asked Nov 17 '18 at 3:58
pankaj babbarpankaj babbar
157
157
I'm fairly certain that I answered this question elsewhere and you have clearly ignored everything I said there. How can I not assume that you would ignore advice provided here too?
– jmcilhinney
Nov 17 '18 at 4:12
hello sir i am new here and i have posted first time here. if you can then pls guide.
– pankaj babbar
Nov 17 '18 at 4:25
As I said, the answer was provided elsewhere. I will say here what I said there. You should should be using a data adapter and aDataTable
. CallFill
on the data adapter to populate theDataTable
and bind it to the grid. Saving the changes is then a matter of callingUpdate
on the data adapter. Of course, you'll need to configure it first, so you should read up on how to do that. I provided this link last time where you could learn how to do that.
– jmcilhinney
Nov 17 '18 at 5:47
@Jimi I am glad you got rid of the field name stupid. :-)
– Mary
Nov 17 '18 at 23:29
add a comment |
I'm fairly certain that I answered this question elsewhere and you have clearly ignored everything I said there. How can I not assume that you would ignore advice provided here too?
– jmcilhinney
Nov 17 '18 at 4:12
hello sir i am new here and i have posted first time here. if you can then pls guide.
– pankaj babbar
Nov 17 '18 at 4:25
As I said, the answer was provided elsewhere. I will say here what I said there. You should should be using a data adapter and aDataTable
. CallFill
on the data adapter to populate theDataTable
and bind it to the grid. Saving the changes is then a matter of callingUpdate
on the data adapter. Of course, you'll need to configure it first, so you should read up on how to do that. I provided this link last time where you could learn how to do that.
– jmcilhinney
Nov 17 '18 at 5:47
@Jimi I am glad you got rid of the field name stupid. :-)
– Mary
Nov 17 '18 at 23:29
I'm fairly certain that I answered this question elsewhere and you have clearly ignored everything I said there. How can I not assume that you would ignore advice provided here too?
– jmcilhinney
Nov 17 '18 at 4:12
I'm fairly certain that I answered this question elsewhere and you have clearly ignored everything I said there. How can I not assume that you would ignore advice provided here too?
– jmcilhinney
Nov 17 '18 at 4:12
hello sir i am new here and i have posted first time here. if you can then pls guide.
– pankaj babbar
Nov 17 '18 at 4:25
hello sir i am new here and i have posted first time here. if you can then pls guide.
– pankaj babbar
Nov 17 '18 at 4:25
As I said, the answer was provided elsewhere. I will say here what I said there. You should should be using a data adapter and a
DataTable
. Call Fill
on the data adapter to populate the DataTable
and bind it to the grid. Saving the changes is then a matter of calling Update
on the data adapter. Of course, you'll need to configure it first, so you should read up on how to do that. I provided this link last time where you could learn how to do that.– jmcilhinney
Nov 17 '18 at 5:47
As I said, the answer was provided elsewhere. I will say here what I said there. You should should be using a data adapter and a
DataTable
. Call Fill
on the data adapter to populate the DataTable
and bind it to the grid. Saving the changes is then a matter of calling Update
on the data adapter. Of course, you'll need to configure it first, so you should read up on how to do that. I provided this link last time where you could learn how to do that.– jmcilhinney
Nov 17 '18 at 5:47
@Jimi I am glad you got rid of the field name stupid. :-)
– Mary
Nov 17 '18 at 23:29
@Jimi I am glad you got rid of the field name stupid. :-)
– Mary
Nov 17 '18 at 23:29
add a comment |
1 Answer
1
active
oldest
votes
- in your first If statement use
OrElse
instead ofOr
. This will stop the evalution as soon as a False is detected. - Keep your connections local. They should be opened at the last minute and closed and disposed as soon as posible. A
Using...End Using
block will take care of that. - If all you need is the count then ask for just the count. Don't retrieve data that you don't use. No
DataReader
necessary. Just useExecuteScalar
. - Good job using Parameters!
- The names of your parameters that you use in your Sql query must match your names when you add the parameters. (Not true for Access where the order of parameter addition is the only thing that matters.)
- Now to the real problem
Dim i As Integer = 0
Then you useDataGridView1.Rows(i).Cells(0).Value
in the For Each loop. Row is always the first row and it never changes in your loop. That is why "it sends only 1 row" - You don't want to keep adding parameters in your
For Each
loop. - One more thing - If
subid
is an auto-increment column do not send any data for this column.
Untested code. I don't have your form or your database.
Private Sub OPCode2()
If TextBox2.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Return
End If
Dim count As Integer
Try
Using cn As New SqlConnection("Your connection string")
Dim thequery As String = "select Count(*) from supplierdata where supmobile = @supmobile;"
Using cmd1 As SqlCommand = New SqlCommand(thequery, cn)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
cn.Open()
count = CInt(cmd1.ExecuteScalar())
End Using
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
If count > 0 Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Return
End If
Try
Using cn As New SqlConnection("Your connection string")
Using cmd2 As New SqlCommand("INSERT INTO supplierdata (supid,supname,supmobile,suploc)VALUES (@supid,@supname,@supmobile,@suploc)", cn)
cmd2.Parameters.Add("@supid", SqlDbType.Int)
cmd2.Parameters.Add("@supname", SqlDbType.NChar)
cmd2.Parameters.Add("@supmobile", SqlDbType.NChar)
cmd2.Parameters.Add("@suploc", SqlDbType.NChar)
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
With cmd2
.Parameters("@supid").Value = row.Cells(0).Value
.Parameters("@supname").Value = row.Cells(1).Value
.Parameters("@supmobile").Value = row.Cells(2).Value
.Parameters("@suploc").Value = row.Cells(3).Value
End With
cmd2.ExecuteNonQuery()
Next
End Using 'You probably want to catch more specific exceptions
'and handle them separately
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
End Sub
EDIT
If supid
is an auto-increment column remove it form the parameters.
Change Insert query to
"INSERT INTO supplierdata (supname,supmobile,suploc)VALUES (@supname,@supmobile,@suploc)"
Then you need to delete the lines
cmd2.Parameters.Add("@supid", SqlDbType.Int)
and
.Parameters("@supid").Value = row.Cells(0).Value
Do this only if supid
is and auto-increment (identity column)
hello sir your code is working fine and inserting all datagridview rows in sql databse .it just gives error before entering data " THE PARAMETERIZED QUERY'(SUPID INT, @SUPNAME NCHAR(4000),@SUPMOBILE NCHAR(4000),@SUPLOC' EXPECTS THE PARAMETER '@SUPID, WHICH WAS NOT SUPPLIED.
– pankaj babbar
Nov 17 '18 at 16:07
If you corrected the code to exclude supid (if it was an auto-increment) please see my edit.
– Mary
Nov 17 '18 at 18:23
i have not set auto incremnet for @supid in sql database. now i have added a line before code on error resume next and it is working fine without giving error.
– pankaj babbar
Nov 21 '18 at 7:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348069%2fhow-to-insert-multiple-rows-from-datagridview-to-sql-database-in-vb-net%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- in your first If statement use
OrElse
instead ofOr
. This will stop the evalution as soon as a False is detected. - Keep your connections local. They should be opened at the last minute and closed and disposed as soon as posible. A
Using...End Using
block will take care of that. - If all you need is the count then ask for just the count. Don't retrieve data that you don't use. No
DataReader
necessary. Just useExecuteScalar
. - Good job using Parameters!
- The names of your parameters that you use in your Sql query must match your names when you add the parameters. (Not true for Access where the order of parameter addition is the only thing that matters.)
- Now to the real problem
Dim i As Integer = 0
Then you useDataGridView1.Rows(i).Cells(0).Value
in the For Each loop. Row is always the first row and it never changes in your loop. That is why "it sends only 1 row" - You don't want to keep adding parameters in your
For Each
loop. - One more thing - If
subid
is an auto-increment column do not send any data for this column.
Untested code. I don't have your form or your database.
Private Sub OPCode2()
If TextBox2.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Return
End If
Dim count As Integer
Try
Using cn As New SqlConnection("Your connection string")
Dim thequery As String = "select Count(*) from supplierdata where supmobile = @supmobile;"
Using cmd1 As SqlCommand = New SqlCommand(thequery, cn)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
cn.Open()
count = CInt(cmd1.ExecuteScalar())
End Using
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
If count > 0 Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Return
End If
Try
Using cn As New SqlConnection("Your connection string")
Using cmd2 As New SqlCommand("INSERT INTO supplierdata (supid,supname,supmobile,suploc)VALUES (@supid,@supname,@supmobile,@suploc)", cn)
cmd2.Parameters.Add("@supid", SqlDbType.Int)
cmd2.Parameters.Add("@supname", SqlDbType.NChar)
cmd2.Parameters.Add("@supmobile", SqlDbType.NChar)
cmd2.Parameters.Add("@suploc", SqlDbType.NChar)
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
With cmd2
.Parameters("@supid").Value = row.Cells(0).Value
.Parameters("@supname").Value = row.Cells(1).Value
.Parameters("@supmobile").Value = row.Cells(2).Value
.Parameters("@suploc").Value = row.Cells(3).Value
End With
cmd2.ExecuteNonQuery()
Next
End Using 'You probably want to catch more specific exceptions
'and handle them separately
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
End Sub
EDIT
If supid
is an auto-increment column remove it form the parameters.
Change Insert query to
"INSERT INTO supplierdata (supname,supmobile,suploc)VALUES (@supname,@supmobile,@suploc)"
Then you need to delete the lines
cmd2.Parameters.Add("@supid", SqlDbType.Int)
and
.Parameters("@supid").Value = row.Cells(0).Value
Do this only if supid
is and auto-increment (identity column)
hello sir your code is working fine and inserting all datagridview rows in sql databse .it just gives error before entering data " THE PARAMETERIZED QUERY'(SUPID INT, @SUPNAME NCHAR(4000),@SUPMOBILE NCHAR(4000),@SUPLOC' EXPECTS THE PARAMETER '@SUPID, WHICH WAS NOT SUPPLIED.
– pankaj babbar
Nov 17 '18 at 16:07
If you corrected the code to exclude supid (if it was an auto-increment) please see my edit.
– Mary
Nov 17 '18 at 18:23
i have not set auto incremnet for @supid in sql database. now i have added a line before code on error resume next and it is working fine without giving error.
– pankaj babbar
Nov 21 '18 at 7:07
add a comment |
- in your first If statement use
OrElse
instead ofOr
. This will stop the evalution as soon as a False is detected. - Keep your connections local. They should be opened at the last minute and closed and disposed as soon as posible. A
Using...End Using
block will take care of that. - If all you need is the count then ask for just the count. Don't retrieve data that you don't use. No
DataReader
necessary. Just useExecuteScalar
. - Good job using Parameters!
- The names of your parameters that you use in your Sql query must match your names when you add the parameters. (Not true for Access where the order of parameter addition is the only thing that matters.)
- Now to the real problem
Dim i As Integer = 0
Then you useDataGridView1.Rows(i).Cells(0).Value
in the For Each loop. Row is always the first row and it never changes in your loop. That is why "it sends only 1 row" - You don't want to keep adding parameters in your
For Each
loop. - One more thing - If
subid
is an auto-increment column do not send any data for this column.
Untested code. I don't have your form or your database.
Private Sub OPCode2()
If TextBox2.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Return
End If
Dim count As Integer
Try
Using cn As New SqlConnection("Your connection string")
Dim thequery As String = "select Count(*) from supplierdata where supmobile = @supmobile;"
Using cmd1 As SqlCommand = New SqlCommand(thequery, cn)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
cn.Open()
count = CInt(cmd1.ExecuteScalar())
End Using
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
If count > 0 Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Return
End If
Try
Using cn As New SqlConnection("Your connection string")
Using cmd2 As New SqlCommand("INSERT INTO supplierdata (supid,supname,supmobile,suploc)VALUES (@supid,@supname,@supmobile,@suploc)", cn)
cmd2.Parameters.Add("@supid", SqlDbType.Int)
cmd2.Parameters.Add("@supname", SqlDbType.NChar)
cmd2.Parameters.Add("@supmobile", SqlDbType.NChar)
cmd2.Parameters.Add("@suploc", SqlDbType.NChar)
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
With cmd2
.Parameters("@supid").Value = row.Cells(0).Value
.Parameters("@supname").Value = row.Cells(1).Value
.Parameters("@supmobile").Value = row.Cells(2).Value
.Parameters("@suploc").Value = row.Cells(3).Value
End With
cmd2.ExecuteNonQuery()
Next
End Using 'You probably want to catch more specific exceptions
'and handle them separately
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
End Sub
EDIT
If supid
is an auto-increment column remove it form the parameters.
Change Insert query to
"INSERT INTO supplierdata (supname,supmobile,suploc)VALUES (@supname,@supmobile,@suploc)"
Then you need to delete the lines
cmd2.Parameters.Add("@supid", SqlDbType.Int)
and
.Parameters("@supid").Value = row.Cells(0).Value
Do this only if supid
is and auto-increment (identity column)
hello sir your code is working fine and inserting all datagridview rows in sql databse .it just gives error before entering data " THE PARAMETERIZED QUERY'(SUPID INT, @SUPNAME NCHAR(4000),@SUPMOBILE NCHAR(4000),@SUPLOC' EXPECTS THE PARAMETER '@SUPID, WHICH WAS NOT SUPPLIED.
– pankaj babbar
Nov 17 '18 at 16:07
If you corrected the code to exclude supid (if it was an auto-increment) please see my edit.
– Mary
Nov 17 '18 at 18:23
i have not set auto incremnet for @supid in sql database. now i have added a line before code on error resume next and it is working fine without giving error.
– pankaj babbar
Nov 21 '18 at 7:07
add a comment |
- in your first If statement use
OrElse
instead ofOr
. This will stop the evalution as soon as a False is detected. - Keep your connections local. They should be opened at the last minute and closed and disposed as soon as posible. A
Using...End Using
block will take care of that. - If all you need is the count then ask for just the count. Don't retrieve data that you don't use. No
DataReader
necessary. Just useExecuteScalar
. - Good job using Parameters!
- The names of your parameters that you use in your Sql query must match your names when you add the parameters. (Not true for Access where the order of parameter addition is the only thing that matters.)
- Now to the real problem
Dim i As Integer = 0
Then you useDataGridView1.Rows(i).Cells(0).Value
in the For Each loop. Row is always the first row and it never changes in your loop. That is why "it sends only 1 row" - You don't want to keep adding parameters in your
For Each
loop. - One more thing - If
subid
is an auto-increment column do not send any data for this column.
Untested code. I don't have your form or your database.
Private Sub OPCode2()
If TextBox2.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Return
End If
Dim count As Integer
Try
Using cn As New SqlConnection("Your connection string")
Dim thequery As String = "select Count(*) from supplierdata where supmobile = @supmobile;"
Using cmd1 As SqlCommand = New SqlCommand(thequery, cn)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
cn.Open()
count = CInt(cmd1.ExecuteScalar())
End Using
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
If count > 0 Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Return
End If
Try
Using cn As New SqlConnection("Your connection string")
Using cmd2 As New SqlCommand("INSERT INTO supplierdata (supid,supname,supmobile,suploc)VALUES (@supid,@supname,@supmobile,@suploc)", cn)
cmd2.Parameters.Add("@supid", SqlDbType.Int)
cmd2.Parameters.Add("@supname", SqlDbType.NChar)
cmd2.Parameters.Add("@supmobile", SqlDbType.NChar)
cmd2.Parameters.Add("@suploc", SqlDbType.NChar)
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
With cmd2
.Parameters("@supid").Value = row.Cells(0).Value
.Parameters("@supname").Value = row.Cells(1).Value
.Parameters("@supmobile").Value = row.Cells(2).Value
.Parameters("@suploc").Value = row.Cells(3).Value
End With
cmd2.ExecuteNonQuery()
Next
End Using 'You probably want to catch more specific exceptions
'and handle them separately
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
End Sub
EDIT
If supid
is an auto-increment column remove it form the parameters.
Change Insert query to
"INSERT INTO supplierdata (supname,supmobile,suploc)VALUES (@supname,@supmobile,@suploc)"
Then you need to delete the lines
cmd2.Parameters.Add("@supid", SqlDbType.Int)
and
.Parameters("@supid").Value = row.Cells(0).Value
Do this only if supid
is and auto-increment (identity column)
- in your first If statement use
OrElse
instead ofOr
. This will stop the evalution as soon as a False is detected. - Keep your connections local. They should be opened at the last minute and closed and disposed as soon as posible. A
Using...End Using
block will take care of that. - If all you need is the count then ask for just the count. Don't retrieve data that you don't use. No
DataReader
necessary. Just useExecuteScalar
. - Good job using Parameters!
- The names of your parameters that you use in your Sql query must match your names when you add the parameters. (Not true for Access where the order of parameter addition is the only thing that matters.)
- Now to the real problem
Dim i As Integer = 0
Then you useDataGridView1.Rows(i).Cells(0).Value
in the For Each loop. Row is always the first row and it never changes in your loop. That is why "it sends only 1 row" - You don't want to keep adding parameters in your
For Each
loop. - One more thing - If
subid
is an auto-increment column do not send any data for this column.
Untested code. I don't have your form or your database.
Private Sub OPCode2()
If TextBox2.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Return
End If
Dim count As Integer
Try
Using cn As New SqlConnection("Your connection string")
Dim thequery As String = "select Count(*) from supplierdata where supmobile = @supmobile;"
Using cmd1 As SqlCommand = New SqlCommand(thequery, cn)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
cn.Open()
count = CInt(cmd1.ExecuteScalar())
End Using
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
If count > 0 Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Return
End If
Try
Using cn As New SqlConnection("Your connection string")
Using cmd2 As New SqlCommand("INSERT INTO supplierdata (supid,supname,supmobile,suploc)VALUES (@supid,@supname,@supmobile,@suploc)", cn)
cmd2.Parameters.Add("@supid", SqlDbType.Int)
cmd2.Parameters.Add("@supname", SqlDbType.NChar)
cmd2.Parameters.Add("@supmobile", SqlDbType.NChar)
cmd2.Parameters.Add("@suploc", SqlDbType.NChar)
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
With cmd2
.Parameters("@supid").Value = row.Cells(0).Value
.Parameters("@supname").Value = row.Cells(1).Value
.Parameters("@supmobile").Value = row.Cells(2).Value
.Parameters("@suploc").Value = row.Cells(3).Value
End With
cmd2.ExecuteNonQuery()
Next
End Using 'You probably want to catch more specific exceptions
'and handle them separately
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
End Sub
EDIT
If supid
is an auto-increment column remove it form the parameters.
Change Insert query to
"INSERT INTO supplierdata (supname,supmobile,suploc)VALUES (@supname,@supmobile,@suploc)"
Then you need to delete the lines
cmd2.Parameters.Add("@supid", SqlDbType.Int)
and
.Parameters("@supid").Value = row.Cells(0).Value
Do this only if supid
is and auto-increment (identity column)
edited Nov 17 '18 at 18:32
answered Nov 17 '18 at 5:14
MaryMary
3,3232818
3,3232818
hello sir your code is working fine and inserting all datagridview rows in sql databse .it just gives error before entering data " THE PARAMETERIZED QUERY'(SUPID INT, @SUPNAME NCHAR(4000),@SUPMOBILE NCHAR(4000),@SUPLOC' EXPECTS THE PARAMETER '@SUPID, WHICH WAS NOT SUPPLIED.
– pankaj babbar
Nov 17 '18 at 16:07
If you corrected the code to exclude supid (if it was an auto-increment) please see my edit.
– Mary
Nov 17 '18 at 18:23
i have not set auto incremnet for @supid in sql database. now i have added a line before code on error resume next and it is working fine without giving error.
– pankaj babbar
Nov 21 '18 at 7:07
add a comment |
hello sir your code is working fine and inserting all datagridview rows in sql databse .it just gives error before entering data " THE PARAMETERIZED QUERY'(SUPID INT, @SUPNAME NCHAR(4000),@SUPMOBILE NCHAR(4000),@SUPLOC' EXPECTS THE PARAMETER '@SUPID, WHICH WAS NOT SUPPLIED.
– pankaj babbar
Nov 17 '18 at 16:07
If you corrected the code to exclude supid (if it was an auto-increment) please see my edit.
– Mary
Nov 17 '18 at 18:23
i have not set auto incremnet for @supid in sql database. now i have added a line before code on error resume next and it is working fine without giving error.
– pankaj babbar
Nov 21 '18 at 7:07
hello sir your code is working fine and inserting all datagridview rows in sql databse .it just gives error before entering data " THE PARAMETERIZED QUERY'(SUPID INT, @SUPNAME NCHAR(4000),@SUPMOBILE NCHAR(4000),@SUPLOC' EXPECTS THE PARAMETER '@SUPID, WHICH WAS NOT SUPPLIED.
– pankaj babbar
Nov 17 '18 at 16:07
hello sir your code is working fine and inserting all datagridview rows in sql databse .it just gives error before entering data " THE PARAMETERIZED QUERY'(SUPID INT, @SUPNAME NCHAR(4000),@SUPMOBILE NCHAR(4000),@SUPLOC' EXPECTS THE PARAMETER '@SUPID, WHICH WAS NOT SUPPLIED.
– pankaj babbar
Nov 17 '18 at 16:07
If you corrected the code to exclude supid (if it was an auto-increment) please see my edit.
– Mary
Nov 17 '18 at 18:23
If you corrected the code to exclude supid (if it was an auto-increment) please see my edit.
– Mary
Nov 17 '18 at 18:23
i have not set auto incremnet for @supid in sql database. now i have added a line before code on error resume next and it is working fine without giving error.
– pankaj babbar
Nov 21 '18 at 7:07
i have not set auto incremnet for @supid in sql database. now i have added a line before code on error resume next and it is working fine without giving error.
– pankaj babbar
Nov 21 '18 at 7:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348069%2fhow-to-insert-multiple-rows-from-datagridview-to-sql-database-in-vb-net%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm fairly certain that I answered this question elsewhere and you have clearly ignored everything I said there. How can I not assume that you would ignore advice provided here too?
– jmcilhinney
Nov 17 '18 at 4:12
hello sir i am new here and i have posted first time here. if you can then pls guide.
– pankaj babbar
Nov 17 '18 at 4:25
As I said, the answer was provided elsewhere. I will say here what I said there. You should should be using a data adapter and a
DataTable
. CallFill
on the data adapter to populate theDataTable
and bind it to the grid. Saving the changes is then a matter of callingUpdate
on the data adapter. Of course, you'll need to configure it first, so you should read up on how to do that. I provided this link last time where you could learn how to do that.– jmcilhinney
Nov 17 '18 at 5:47
@Jimi I am glad you got rid of the field name stupid. :-)
– Mary
Nov 17 '18 at 23:29