Click here to Skip to main content
15,063,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView I am loading Data into with my Web Service. What I want to be able to do is add another line under the already saved and loaded data and then save only that one line.

I can get it to load new data just fine and if there is no data I can get it to save fine. But when I save new data when there is already data in the Grid, it saves another set of the saved data plus my new data. So if there were 2 saved rows and I added 1 row now there are 3 rows that saved making 5 records rather than 1 making a total of 3 saved records.

What I need to figure out is how do I insert a new record only and keep from duplicating the records that already exist. I want to avoid using a button to add new items and clear the data as it is causing issues.

Here is my code for Saving the rows

VB
Sub InsertAir()
Dim ROW_ID As Integer
 Dim TEST_ID As String = txtTestID.Text
        Dim AIRTYPE As String
        Dim AIR_PRESS As String
        Dim RESULTS As String
        Dim PASS As String
        Dim METHOD As String
        Dim NOTES As String
        Cursor = Cursors.No
        Dim x As Integer
        For x = 0 To dgAir.Rows.Count - 2
            GetRowID()
            ROW_ID = Val(txtRowID.Text)
            AIRTYPE = dgAir.Rows(x).Cells(0).Value
            AIR_PRESS = dgAir.Rows(x).Cells(1).Value
            RESULTS = dgAir.Rows(x).Cells(2).Value
            PASS = dgAir.Rows(x).Cells(3).Value
            METHOD = dgAir.Rows(x).Cells(4).Value

            Dim sql As String = "INSERT INTO TEST_LAB_TAB_AIR (ROW_ID, "
            sql = sql & "                              TEST_ID, "
            sql = sql & "                              TYPE, "
            sql = sql & "                              PRESSURE, "
            sql = sql & "                              RESULT, "
            sql = sql & "                              PASS_FAIL, "
            sql = sql & "                              TEST_METHOD) VALUES (" & ROW_ID & "," & TEST_ID & "," & _
                    "'" & AIRTYPE & "','" & AIR_PRESS & "','" & RESULTS & "','" & PASS & "','" & _
                    "" & METHOD & "')"
            Dim s As String
            Dim TestLab As New TestLab.ServiceSoapClient("ServiceSoap")
            s = TestLab.InsertTabs(sql)
        Next
End Sub


And here is where I am loading the Data into the Grid.

VB
Sub GetAir()
        Dim i As Integer = CInt(gv.SelectedCells(0).Value.ToString)
        On Error GoTo 10

        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim Row As DataRow

        Dim TestLab As New TestLab.ServiceSoapClient("ServiceSoap")
        ds = TestLab.GetAir(i)
        Row = ds.Tables("ImageFile").Rows(0)
10:
        If Err.Number <> 0 Then
            Err.Clear()
            dt.Clear()
            ds.Clear()
        End If
        dt = ds.Tables(0)
        System.Web.HttpContext.Current.Session("MyData") = dt
        dgAir.DataSource = dt

    End Sub
Any help would be appreciated!!!!!!
Thanks
Posted
Updated 6-Apr-12 11:33am
v2
Comments
Shahin Khorshidnia 6-Apr-12 18:46pm
   
Why don't you have a primary key in the database? Primary key prevents the database for duplicating an ID. For example you must set the Test_ID as primary key.
vaibhav mahajan 7-Apr-12 3:02am
   
DATASET CAN BE THE ANSWER

Finally figured it out. here is my code

VB
For x = 0 To dgAir.Rows.Count - 2
            Dim w As String = dgAir.Rows(x).Cells("AirID").Value.ToString
            If w = "" Then
                GetRowID()
                AIRTYPE = dgAir.Rows(x).Cells("AirType").Value
                AIR_PRESS = dgAir.Rows(x).Cells("AirPress").Value
                RESULTS = dgAir.Rows(x).Cells("AirResult").Value
                PASS = dgAir.Rows(x).Cells("AirPass").Value
                METHOD = dgAir.Rows(x).Cells("AirMethod").Value
                NOTES = dgAir.Rows(x).Cells("AirNotes").Value.ToString
                ROW_ID = Val(txtRowID.Text)

                Dim sql As String = "INSERT INTO TEST_LAB_TAB_AIR (ROW_ID, "
                sql = sql & "                              TEST_ID, "
                sql = sql & "                              TYPE, "
                sql = sql & "                              PRESSURE, "
                sql = sql & "                              RESULT, "
                sql = sql & "                              PASS_FAIL, "
                sql = sql & "                              TEST_METHOD, "
                sql = sql & "                              NOTES) VALUES (" & ROW_ID & "," & TEST_ID & "," & _
                        "'" & AIRTYPE & "','" & AIR_PRESS & "','" & RESULTS & "','" & PASS & "','" & _
                        "" & METHOD & "','" & NOTES & "')"
                Dim s As String
                Dim TestLab As New TestLab.ServiceSoapClient("ServiceSoap")
                s = TestLab.InsertTabs(sql)
            End If
        Next


I ended up having to put my code into an if statment that looked for the Row_ID and if it was blank yet then it added it but if the item existed, then it skipped to the next item.

Hope this helps somone out, was a real pain to get to work!!
   
I have a Primary key in the Database called Row_id, BUT, When I save it is getting a New Row_ID for each row in the table. I need to somehow capture that it already exists and then skip over those records. Any suggestions on how to do this? I know you can do an If Statment and For loops but I am not sure on how to code it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900