Hi all. Could someone pls tel me whats wrong with my code for adding new records to the db. Nothing is being added to the db and before the form opens up when running it, it brings an error that the file name of my form is invalid. Could someone pls help in this problem. Its weighing me down!

Code in the form load
strsql = "SELECT * FROM Landlord ORDER BY Landlord ASC"
Dim cmd As New OleDbCommand(strsql, oledbcon)
da = New OleDbDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "Landlord")
dt = ds.Tables(0)
txtid.DataBindings.Add("Text", dt, "LandlordID")
txtplot.DataBindings.Add("Text", dt, "Plot No")
txtname.DataBindings.Add("Text", dt, "Name")
txtaddress.DataBindings.Add("Text", dt, "Address")
txttel.DataBindings.Add("Text", dt, "Tel No")
txtbank.DataBindings.Add("Text", dt, "Bank")
txtac.DataBindings.Add("Text", dt, "A/C No")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try

Code in the button Add
Try
If flag Then
strsql = "INSERT INTO Landlord(LandlordID,Plot No,Name,Address,Tel No,Bank,A/C No)" & _
"VALUES ('" & txtid.Text & "','" & txtplot.Text & "','" & txtname.Text & "','" & txtaddress.Text & "','" & txttel.Text & "','" & txtbank.Text & "','" & txtac.Text & "')"
oledbcon.Open()
Dim cmd As New OleDbCommand(strsql, oledbcon)
cmd.ExecuteNonQuery()
oledbcon.Close()
End If
cur = New Landlord
cur.Show()
Me.Hide()
MsgBox("Data Added Successfully !", MsgBoxStyle.Information, Me.Text)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try

Maybe you have used a keyword, space, changed the file name.

Remove the Exception handling block and specify the Error that occurrs to enable me to evaluate the problem.

Hi,

The first error, it usually is generated when u have Re-Named the form in the Solution Explorer. Its a good idea to change the name of the form in Property Sheet.

Regrading second problem, check if u have a Primary/Unique Key for the ID column. If key is there and u r trying to save duplicate / null value, then u may not get any error but, data is not saved in database.

Remove the Try-Catch and check for any error.

Regards
Veena

This article has been dead for over six months. Start a new discussion instead.