View Full Version : Vb.net Help-saving to database


shelly121
06-03-2005, 14:51
hey ppl, anyone help, iv done code for a save button to save data from vb.net to microsoft access, but when the form is playing and i press save it says the following error:

object reference not set to an instance of an object

????what on earth does that mean!!! :)

JoeP
06-03-2005, 15:03
What it says.... :)

You've got something dimmed as an Object and haven't set it to be an instance of the object of the specified type.

Care to post the code?

Joe

shelly121
07-03-2005, 10:09
Originally posted by JoePritchard
What it says.... :)

You've got something dimmed as an Object and haven't set it to be an instance of the object of the specified type.

Care to post the code?

Joe

the code for the save button is:

Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If txtFirstName.Text = "" Then
MsgBox("Please Fill in First Name", MsgBoxStyle.Critical)
ElseIf txtLastName.Text = "" Then
MsgBox("Please fill in Last Name", MsgBoxStyle.Critical)
ElseIf cmbDay.Text = "" Or cmbMonth.Text = "" Or cmbYear.Text = "" Then
MsgBox("Please fill The date of birth correctly", MsgBoxStyle.Critical)
ElseIf txtPostcode.Text = "" Then
MsgBox("Please fill in the Postcode Field", MsgBoxStyle.Critical)
ElseIf txtHouseNo.Text = "" Then
MsgBox("Please fill in the Current House Number", MsgBoxStyle.Critical)
ElseIf txtNewHouseNo.Text = "" Then
MsgBox("Please fill in the New House Number", MsgBoxStyle.Critical)
ElseIf txtNewPostcode.Text = "" Then
MsgBox("Please fill in the New Postcode", MsgBoxStyle.Critical)
ElseIf txtDeposit.Text = "" Then
MsgBox("Please fill in the Deposit Field", MsgBoxStyle.Critical)
ElseIf txtDate.Text = "" Then
MsgBox("Please fill in the Date of purchase", MsgBoxStyle.Critical)
ElseIf txtPrice.Text = "" Then
MsgBox("Please fill in the Property price field", MsgBoxStyle.Critical)
ElseIf txtType.Text = "" Then
MsgBox("Please fill in the Type of mortgage", MsgBoxStyle.Critical)
End If

Dim fName As String
fName = Me.txtNewPostcode.Text
If fName.Length < 2 Then
Return
Else
fName = fName.Substring(0, 2).ToUpper
End If

If fName = "LL" Then
Try
Dim myDataRow As DataRow
myDataRow = Me.DsClients1.Tables("tblClients").NewRow()
myDataRow("First Name") = Me.txtFirstName.Text
myDataRow("Last Name") = Me.txtLastName.Text
myDataRow("day(DOB)") = Convert.ToDateTime(Me.cmbDay.Text)
myDataRow("Month(DOB)") = Convert.ToDateTime(Me.cmbMonth.Text)
myDataRow("Year(DOB)") = Convert.ToDateTime(Me.cmbYear.Text)
myDataRow("Current House Number") = Me.txtHouseNo.Text
myDataRow("Current PostCode") = Me.txtPostcode.Text
myDataRow("Postcode of property to be purchased") = Me.txtPostcode.Text
myDataRow("House No' of property to be purchased") = Me.txtPostcode.Text
myDataRow("Price of property") = Me.txtPrice.Text
myDataRow("Type Of Mortgage") = Me.txtType.Text
myDataRow("Amount of deposit:") = Me.txtDeposit.Text
myDataRow("Date of purchase") = Me.txtDate.Text
'dob in the database is of type datetime
Me.DsClients1.Tables("tblClient").Rows.Add(myDataRow)
Me.daClients.Update(Me.DsClients1, "tblClient")
Me.DsClients1.AcceptChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try

Else
Dim fw As StreamWriter
Dim fn As String
fn = fName & ".txt"
'MsgBox(Application.StartupPath)
fw = New StreamWriter("c:\temp\" & fn, True)
fw.Write(Me.txtFirstName.Text & ",")
fw.Write(Me.txtLastName.Text & ",")
fw.Write(Me.cmbDay.Text & ",")
fw.Write(Me.cmbMonth.Text & ",")
fw.Write(Me.cmbYear.Text & ",")
fw.Write(Me.txtHouseNo.Text & ",")
fw.Write(Me.txtNewPostcode.Text & ",")
fw.Write(Me.txtNewHouseNo.Text & ",")
fw.Write(Me.txtPrice.Text & ",")
fw.Write(Me.txtType.Text & ",")
fw.Write(Me.txtDeposit.Text & ",")
fw.Write(Me.txtDate.Text & ",")
fw.WriteLine(Me.txtPostcode.Text)
fw.Close()
End If
End Sub

D2J
07-03-2005, 13:58
All that for one button :|

I think I best get swotting or I will never get upto speed :gag:

JoeP
07-03-2005, 14:05
Shelly,

Which line is highlighted in the error message ?

Joe

adaline
07-03-2005, 14:07
Damn vb is soooo messy :gag:

shelly121
07-03-2005, 15:49
Originally posted by JoePritchard
Shelly,

Which line is highlighted in the error message ?

Joe

wel thats the thing, it doesnt say theres an error when i click to open/play the form,but when i play/open it everyhting works until i press the "save" button, and then it comes up with an error msg saying what i mentioned above. but i doesnt close teh form, i can just press ok and it just means it hasnt saved i spose do to an error????

adaline
07-03-2005, 16:09
When u run ur form and it comes up with an error, click on debug and it will take u to the line on which the error came up.

shelly121
07-03-2005, 16:09
Originally posted by Deejay
All that for one button :|

I think I best get swotting or I will never get upto speed :gag:

DeeJay, thats just becus iv got so many text boxes, that i need each one saved to my database!!!

JoeP
07-03-2005, 16:26
You beat me to it, adaline.

Press Debug and see what line gets highlighted.

Shelley, your teachers are not impressing me at this point! :)

shelly121
07-03-2005, 17:14
Originally posted by JoePritchard
You beat me to it, adaline.

Press Debug and see what line gets highlighted.

Shelley, your teachers are not impressing me at this point! :)

right wel it was debugging from the point i press start but not highlighting anything, so i then clicked "break all" and it highlighted " MsgBox(ex.Message)" in the bit below:


Catch ex As Exception
MsgBox(ex.Message)
End Try

and as for my teacher, it could just be me not bein very clever :) but i dunno, as it was ment to be in last wk, and about 20 students out of about 25 havent finished it!! so i doubt it its me!

adaline
07-03-2005, 21:56
Lol. Well were any of the textboxes empty when error comes up?
Those "Convert" lines maybe doing the trouble if thers nothing to parse....

shelly121
08-03-2005, 16:37
Originally posted by adaline
Lol. Well were any of the textboxes empty when error comes up?
Those "Convert" lines maybe doing the trouble if thers nothing to parse....

hey thanks both of u. no none of the text boxes r empty- but dunna worry- il email the lecturer n ask him to look at it 2mora! thankz agen. shelly