Help with updating data for SQL using a data grid view in VB.Net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, I am using VB.Net and SQL. I am trying to select data in a grid view that is retrieved from a table in SQL and pass it to a text box. Once the data is passed to a text box, I will like to be able to edit the data in the text box and update the existing
table in SQL. I have successful in passing the data to the text box but each time I try to update the data in the text box I receive and error: “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index”.
The line of code that causes the error is “<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@JobID", SqlDbType.Int).Value = <span style="color:blue
CInt(dgvJobs.SelectedRows(0).Cells(0).Value.
The full code that is causing the error in VB.net is listed below:

<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Private <span style="color:blue Sub UpdateJob()
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Dim sqlCmd <span style="color:blue As
<span style="color:blue New SqlCommand
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:green * Set up SQL Command pointing to the add order details SP
<span style="font-family:Courier New; font-size:10pt <span>
sqlCmd.Connection = sqlConEdit
<span style="font-family:Courier New; font-size:10pt <span>
sqlCmd.CommandText = <span style="color:#a31515 "up_Update_Jobs"<span>
<span style="color:green * Point to SP
<span style="font-family:Courier New; font-size:10pt <span>
sqlCmd.CommandType = CommandType.StoredProcedure
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:green * Process
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue With sqlCmd.Parameters
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@JobID", SqlDbType.Int).Value = <span style="color:blue
CInt(dgvJobs.SelectedRows(0).Cells(0).Value)
<span style="font-family:Courier New; font-size:10pt <span>
<span> .Add(<span style="color:#a31515 "@JobTitle", SqlDbType.NVarChar, 30).Value = txtJobTitle.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@JobType", SqlDbType.NVarChar, 20).Value = cboJobType.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@Availability", SqlDbType.NVarChar, 20).Value = cboAvailability.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@Industry", SqlDbType.NVarChar, 30).Value = cboIndustry.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@ContactName", SqlDbType.NVarChar, 40).Value = txtContactName.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@ContactPhone", SqlDbType.NVarChar, 20).Value = txtPhone.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@ContactEmail", SqlDbType.NVarChar, 30).Value = txtEmail.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@Address", SqlDbType.NVarChar, 50).Value = txtAddress.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@City", SqlDbType.NVarChar, 30).Value = txtCity.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@State", SqlDbType.NVarChar, 30).Value = txtState.Text
<span style="font-family:Courier New; font-size:10pt <span>
.Add(<span style="color:#a31515 "@ZipCode", SqlDbType.NVarChar, 20).Value = txtZipCode.Text
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:green * Execute the stored procedure to create the new order
<span style="font-family:Courier New; font-size:10pt <span>
sqlCmd.ExecuteNonQuery()
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:green * Clear the parameters collection
<span style="font-family:Courier New; font-size:10pt <span>
sqlCmd.Parameters.Clear()

<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue With

<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Sub

<span>The SP for this code is as follow:

<span style="color:blue; font-family:Courier New; font-size:10pt ALTER<span style="font-family:Courier New; font-size:10pt
<span style="color:blue PROCEDURE [dbo]<span style="color:gray .[up_Update_Jobs]

<span style="font-family:Courier New; font-size:10pt @JobID <span style="color:blue
int<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt @CompanyName <span style="color:blue
nvarchar<span style="color:gray (40<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @JobTitle <span style="color:blue
nvarchar<span style="color:gray (20<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @JobType <span style="color:blue
nvarchar<span style="color:gray (20<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @Availability <span style="color:blue
nvarchar<span style="color:gray (20<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @Industry <span style="color:blue
nvarchar<span style="color:gray (30<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @ContactName <span style="color:blue
nvarchar<span style="color:gray (40<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @ContactPhone <span style="color:blue
nvarchar<span style="color:gray (20<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @ContactEmail <span style="color:blue
nvarchar<span style="color:gray (30<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @Address <span style="color:blue
nvarchar<span style="color:gray (50<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @City <span style="color:blue
nvarchar<span style="color:gray (30<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @State <span style="color:blue
nvarchar<span style="color:gray (30<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @ZipCode <span style="color:blue
nvarchar<span style="color:gray (20<span style="color:gray ),
<span style="font-family:Courier New; font-size:10pt @Website <span style="color:blue
nvarchar<span style="color:gray (30<span style="color:gray )

<span style="color:blue; font-family:Courier New; font-size:10pt AS

<span style="color:blue; font-family:Courier New; font-size:10pt UPDATE<span style="font-family:Courier New; font-size:10pt JobInfo

<span style="color:blue; font-family:Courier New; font-size:10pt SET<span style="font-family:Courier New; font-size:10pt CompanyName
<span style="color:gray = @CompanyName<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
JobTitle <span style="color:gray = @JobTitle<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
JobType <span style="color:gray = @JobType<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
Availability <span style="color:gray = @Availability<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
Industry <span style="color:gray = @Industry<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
ContactName <span style="color:gray = @ContactName<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
ContactPhone <span style="color:gray = @ContactPhone<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
ContactEmail <span style="color:gray = @ContactEmail<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Address <span style="color:gray = @Address<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
City <span style="color:gray = @City<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue State <span style="color:gray = @State<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
ZipCode <span style="color:gray = @ZipCode<span style="color:gray ,
<span style="font-family:Courier New; font-size:10pt <span>
Website <span style="color:gray = @Website
<span style="font-family:Courier New; font-size:10pt <span>

<span style="color:blue; font-family:Courier New; font-size:10pt WHERE<span style="font-family:Courier New; font-size:10pt JobID
<span style="color:gray = @JobID

View the full article
 
Back
Top