Add And Then Modify

.netdev

Member
Joined
Jan 7, 2006
Messages
10
Programming Experience
Beginner
here is my modification button code :

Private Sub btnmodify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmodify.Click
btnsave.Visible =
True
cmdRequery.Visible = True
txtfirstname.Enabled = True
txtlastname.Enabled = True
ComboBox1.Enabled = True
txttitlec.Enabled = True
txtadress.Enabled = True
txtpostalcode.Enabled = True
txtnotes.Enabled = True
txthomephone.Enabled = True
txtextension.Enabled = True
cmbcountry.Enabled = True
Cmbcity.Enabled = True
cmbregion.Enabled = True
birthdate.Enabled = True
Hiredate.Enabled = True
Dim pDataRow As DataRow
pDataRow = mDataSet.Tables(0).Rows(
CInt(txtCurrent.Text) - 1)
pDataRow!FirstName = txtfirstname.Text
pDataRow!LastName = txtlastname.Text
pDataRow!SaveAsName = txtfirstname.Text + " " + txtlastname.Text
'pDataRow!SaveAsName = txtsaveas.Text
pDataRow!Title = ComboBox1.Text
pDataRow!TitleOfCourtesy = txttitlec.Text
pDataRow!Address = txtadress.Text
cmbcountry.Text =
CStr(pDataRow!Country)
Cmbcity.Text =
CStr(pDataRow!City)
cmbregion.Text =
CStr(pDataRow!Region)

txtpostalcode.Text =
CStr(pDataRow!PostalCode)
txtextension.Text =
CStr(pDataRow!Extension)
txthomephone.Text =
CStr(pDataRow!HomePhone)
txtnotes.Text =
CStr(pDataRow!Notes)
'ADDDS
'am adding hereeeeeeeeeeeeeeee
Dim h
h = txtreport.SelectedValue
' MsgBox("value selected id " & h)
' TextBox3.Text = h
pDataRow!ReportsTo = h
'END ADDS

pDataRow!BirthDate = birthdate.Text
pDataRow!HireDate = Hiredate.Text
btnsave.Enabled =
True
end sub

and here is the save button that i click after the button modify has been clicked :

Dim pDataRow As DataRow
pDataRow = mDataSet.Tables(0).Rows(
CInt(txtCurrent.Text) - 1)
pDataRow!FirstName = txtfirstname.Text
pDataRow!LastName = txtlastname.Text
pDataRow!SaveAsName = txtsaveas.Text
pDataRow!Title = ComboBox1.Text
pDataRow!TitleOfCourtesy = txttitlec.Text
pDataRow!Address = txtadress.Text
pDataRow!Country = cmbcountry.Text
pDataRow!City = Cmbcity.Text
pDataRow!Region = cmbregion.Text
pDataRow!PostalCode = txtpostalcode.Text
pDataRow!HomePhone = txthomephone.Text
pDataRow!Extension = txtextension.Text
pDataRow!Notes = txtnotes.Text
pDataRow!BirthDate = birthdate.Text
pDataRow!HireDate = Hiredate.Text
pDataRow!SaveAsName = txtfirstname.Text + " " + txtlastname.Text
'ADDDS
'am adding hereeeeeeeeeeeeeeee
' pDataRow!ReportsTo = TextBox4.Text
Dim h
h = txtreport.SelectedValue
' MsgBox("value selected id " & h)
' TextBox3.Text = h
pDataRow!ReportsTo = h
'END ADDS


Try
mDataAdapter.Update(mDataSet)
MsgBox("RECORD SUCCESSFULLY ADDED TO Database", MsgBoxStyle.Information, "Sucess")
Catch eror As Exception
MsgBox("This was an error updating database." & vbCrLf & _
eror.Message)
If txtCurrent.Text = txtCount.Text Then
btnFirst.Enabled = True
btnPrevious.Enabled = True
btnNext.Enabled = False
BtnLast.Enabled = False
End If
End Try
btnsave.Visible = False
cmdRequery.Visible = False
end sub




THE PROBLEM IS THAT I CAN'T MODIFY THE LAST RECORD ONCE I ADD AND TRIES TO MODIFY AFTER ITT


THIS IS THE ADD BUTTON CODE


txtfirstname.Focus()
btnsave.Visible =
True
cmdRequery.Visible = True
txtfirstname.Text = ""
txtlastname.Text = ""
txttitlec.Text = "Title Of Courtesy"
txtadress.Text = ""
txtpostalcode.Text = ""
txtextension.Text = ""
txthomephone.Text = ""
txtnotes.Text = ""
cmbregion.Text = ""
Cmbcity.Text = ""
txtreport.Text = ""

txtfirstname.Enabled =
True
txtlastname.Enabled = True
txttitlec.Enabled = True
ComboBox1.Enabled = True
cmbcountry.Enabled = True
txtadress.Enabled = True
txtnotes.Enabled = True
txtpostalcode.Enabled = True
txtextension.Enabled = True
txthomephone.Enabled = True
birthdate.Enabled = True
Hiredate.Enabled = True
Cmbcity.Enabled = True
cmbregion.Enabled = True
txtreport.Enabled = True





Dim pDataRow As DataRow
' this is how to instantiate a new DataRow object: from its parent
pDataRow = mDataSet.Tables(0).NewRow()
pDataRow!FirstName = txtfirstname.Text
pDataRow!LastName = txtlastname.Text
pDataRow!Title = ComboBox1.Text
pDataRow!TitleOfCourtesy = txttitlec.Text
pDataRow!Address = txtadress.Text
pDataRow!SaveAsName = txtsaveas.Text

pDataRow!Country = cmbcountry.Text
pDataRow!City = Cmbcity.Text
pDataRow!Region = cmbregion.Text
pDataRow!PostalCode = txtpostalcode.Text
pDataRow!HomePhone = txthomephone.Text
pDataRow!Extension = txtextension.Text
pDataRow!Notes = txtnotes.Text
pDataRow!Extension = txtextension.Text
pDataRow!BirthDate = birthdate.Text
pDataRow!HireDate = Hiredate.Text
'pDataRow!ReportsTo = TextBox4.Text


' the NewRow method called above will just instantiate a new DataRow object,
' it will not append the NewRow to the DataTable
mDataSet.Tables(0).Rows.Add(pDataRow)
txtCount.Text =
CStr(CInt(txtCount.Text) + 1)
btnsave.Enabled =
True ' let user update the underlying database
txtCurrent.Text = txtCount.Text ' make the current row, the last (recently added) row

btnFirst.Enabled = True
btnPrevious.Enabled = True
btnNext.Enabled = False
BtnLast.Enabled = False
ComboBox1.Text = "Choose Employee Title"
cmbcountry.Text = "Select Country"
txtreport.Text = "Employee Supervisor"
'system date
birthdate.Text = Now()
Hiredate.Text = Now()
' Me.Refresh()
mConn.Close()
End Sub


AND ALSO AFTER THE ADD THE SAME SAVE BUTTON IS CLIKED TO SAVE

 
There's no way I'm reading that much code and I'd guess that many, if not most, others will be the same. You need to narrow the issue down a bit. You say you can't do something, so what happens? Is an exception thrown? If so, what line is it thrown on and what is the error message? Have you used the debugger properly? have you set a breakpoint and stepped through your code line by line?
 
Here's a quick piece of advice and i agree with Jmcilhinney, reading through that much code would do anyone's head in!! Comment out all the enabled stuff and all the other bits that are not neccessary to update your database. Then post back with just that code and a description of any error messages, if any. In closing you could make this code more concise by changing this bit...

VB.NET:
Dim pDataRow As DataRow
' this is how to instantiate a new DataRow object: from its parent
pDataRow = mDataSet.Tables(0).NewRow()
pDataRow!FirstName = txtfirstname.Text
pDataRow!LastName = txtlastname.Text
pDataRow!Title = ComboBox1.Text
pDataRow!TitleOfCourtesy = txttitlec.Text
pDataRow!Address = txtadress.Text
pDataRow!SaveAsName = txtsaveas.Text

pDataRow!Country = cmbcountry.Text
pDataRow!City = Cmbcity.Text
pDataRow!Region = cmbregion.Text
pDataRow!PostalCode = txtpostalcode.Text
pDataRow!HomePhone = txthomephone.Text
pDataRow!Extension = txtextension.Text
pDataRow!Notes = txtnotes.Text
pDataRow!Extension = txtextension.Text
pDataRow!BirthDate = birthdate.Text
pDataRow!HireDate = Hiredate.Text
'pDataRow!ReportsTo = TextBox4.Text

to...

in one of the textbox's leave event add the handles clause for all the texbox's above. Then put this in the event...

VB.NET:
Dim Tb as textbox = Ctype(sender,textbox)
If not Tb.text = String.empty then
pdatarow(tb.name) = tb.text
End If

of course this will only work if the name of your textbox is the same as the column name in your datatable.
 
Back
Top