Wrong Record gettong Modified

kbsudhir

Member
Joined
Jun 13, 2008
Messages
21
Programming Experience
Beginner
I am using VB 2008 with SQL 2008.

When I add a new record which is working flawlessly but along with that the code will modify the the previous record to the same which I just updated.

I have no idea why this is happening.

Below is the code which I am using to create a new record.

VB.NET:
Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBtn.Click

 flag = 0

        SrTxt.Text = ""
        TitTxt.Text = ""
        MediaTxt.Text = ""
        GenreTxt.Text = ""
        DescTxt.Text = ""
        CostTxt.Text = ""
        LangTxt.Text = ""
        RatingsTxt.Text = ""
        StaringTxt.Text = ""
        DiscTxt.Text = ""
        StatusTxt.Text = ""

        flag = 1

        Dim len As Integer

        dt = CollectionDataSet1.CD_DVDCollection
        If dt.Rows.Count = 0 Then

            SrlNo = 1
            SrTxt.Text = SrlNo

        Else

            len = dt.Rows.Count - 1
            dr = dt.Rows(len)
            SrlNo = dr("Sl_No")
            SrlNo = CInt(SrlNo) + 1
            SrTxt.Text = SrlNo
            SerialNo = SrlNo

        End If

        SrTxt.Enabled = False
        TitTxt.Enabled = True
        MediaTxt.Enabled = True
        GenreTxt.Enabled = True
        DescTxt.Enabled = True
        CostTxt.Enabled = True
        LangTxt.Enabled = True
        RatingsTxt.Enabled = True
        StaringTxt.Enabled = True
        DiscTxt.Enabled = True
        StatusTxt.Enabled = True



    End Sub

Below is teh code which I am using ti save any changes done to the dataset
i.e Add, Modify or delete.

VB.NET:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
SrChk:
        If flag = 1 Then

            If SerialNo = SrTxt.Text Then

                dt = CollectionDataSet1.CD_DVDCollection

                dr = dt.NewRow()

                dr("Sl_No") = CInt(SrTxt.Text)
                dr("Titles") = TitTxt.Text
                dr("MediaType") = MediaTxt.Text
                dr("Genre") = GenreTxt.Text
                dr("Brief_desc") = DescTxt.Text
                dr("Cost") = CostTxt.Text
                dr("Language") = LangTxt.Text
                dr("Rating") = RatingsTxt.Text
                dr("Starring") = StaringTxt.Text
                dr("Disc_No") = DiscTxt.Text
                dr("Current_Status") = StatusTxt.Text

                dt.Rows.Add(dr)

                SrTxt.Enabled = True
            Else
                SrTxt.Text = SerialNo
                GoTo SrChk
            End If

        ElseIf flag = 2 Then
            dr.Delete()

        ElseIf flag = 3 Then

            dt = CollectionDataSet1.CD_DVDCollection


            dr = dt.Rows.Find(SrlNo)

            dr.BeginEdit()

            dr("Sl_No") = CInt(SrTxt.Text)
            dr("Titles") = TitTxt.Text
            dr("MediaType") = MediaTxt.Text
            dr("Genre") = GenreTxt.Text
            dr("Brief_Desc") = DescTxt.Text
            dr("Cost") = CostTxt.Text
            dr("Language") = LangTxt.Text
            dr("Rating") = RatingsTxt.Text
            dr("Starring") = StaringTxt.Text
            dr("Disc_No") = DiscTxt.Text
            dr("Current_Status") = StatusTxt.Text

            dr.EndEdit()
            SrTxt.Enabled = True


        End If

        Me.CD_DVDCollectionTableAdapter.Update(CollectionDataSet1.CD_DVDCollection)

        SrTxt.Enabled = False
        TitTxt.Enabled = False
        MediaTxt.Enabled = False
        GenreTxt.Enabled = False
        DescTxt.Enabled = False
        CostTxt.Enabled = False
        LangTxt.Enabled = False
        RatingsTxt.Enabled = False
        StaringTxt.Enabled = False
        DiscTxt.Enabled = False
        StatusTxt.Enabled = False

        flag = 0

        MsgBox("Updated")

    End Sub

Please let me know where I am going wrong.

Regards
Sudhir
 
Why not bind your dataset to your text boxes? You're already doing your data access a sensible way, but this will make some things a lot easier for you to code..

Check out the tutorials in the DW2 link in my signature..
 
I have already done that.
& I am using the buttons to navigate between the records, which is also working fine.

But only problem is when I try to add a new record.
 
There is no way, he you had read the tutorial i pointed you to that you would have written all that code, nor would you be having trouble adding a record because it is done automatically for you. I suggest you start a new project and follow the tutorial.. You will achieve in about 15 minutes what you've been struggling with for 3 days. It really is that easy to bin what you've written and start over for a better, faster result.
 
Back
Top