Insert data in vb.net

Aditya

New member
Joined
Jul 22, 2006
Messages
4
Programming Experience
3-5
I'm facing a problem in inserting data from textboxes into an access database. All my coding is according to book and no error is there still at run time it throws exception and doesn't enter data. I haven't done it through coding i've picked everything from data tab in toolbox. .NET has its self generated code for that i've just added following lines. I need help quickly. Following is the code:

Dim query As String
query = Me.OleDbInsertCommand1.CommandText
Dim cmd As New OleDb.OleDbCommand(query, OleDbConnection1)
OleDbConnection1.Open()
cmd.ExecuteNonQuery()
'This command will insert the data.
MsgBox("Data Entered Successfully")
OleDbConnection1.Close()
 
I've got this code working but it is not inserting any data in the database. After execution rows in the table increase but no data can be seen i.e. blank rows get inserted. This is a trial application code coz i have to implement this in a large database application. Can anybody help me?

Me.OleDbInsertCommand1.CommandText = "INSERT INTO anu1(age, name) VALUES (" & Val(TextBox2.Text) & ", '" & TextBox1.Text & "')"
Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
OleDbConnection1.Open()
OleDbInsertCommand1.ExecuteNonQuery()
'This command will insert the data.
MsgBox("Data Entered Successfully")
OleDbConnection1.Close()
 
Last edited:
That in my opinion is the prblem with the generated code bit, if you don't understand what it's done, then how can you fix it if it doesn't work. That said, there's no reason why what you have done shouldn't work based on what you've posted. I'm guessing you are getting the highly descriptive OleDbException being thrown, i take it you have caught the exception and displayed it to yourself in a messagebox?. Other than that it would be difficult to advise from here, without seeing more of your code.
 
Ok. You wanted to see more code. Here it is.
I've got this code working now but it is not inserting any data in the database. After execution rows in the table increase but no data can be seen i.e. blank rows get inserted. This is a trial application code coz i have to implement this in a large database application. Can u help me?

Me.OleDbInsertCommand1.CommandText = "INSERT INTO anu1(age, name) VALUES (" & Val(TextBox2.Text) & ", '" & TextBox1.Text & "')"
Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
OleDbConnection1.Open()
OleDbInsertCommand1.ExecuteNonQuery()
'This command will insert the data.
MsgBox("Data Entered Successfully")
OleDbConnection1.Close()

 
VB.NET:
        Me.OleDbInsertCommand1 = New OleDbCommand("INSERT INTO anu1 (age, name) VALUES (?, ?)", Me.OleDbConnection1)
        With Me.OleDbInsertCommand1.Parameters
            If IsNumeric(TextBox2.Text.Trim) = True Then
                .Add(New OleDbParameter("age", OleDbType.Double)).Value = Double.Parse(TextBox2.Text.Trim)
            End If
            .Add(New OleDbParameter("name", OleDbType.VarWChar)).Value = TextBox1.Text.Trim
        End With
        OleDbConnection1.Open()
        OleDbInsertCommand1.ExecuteNonQuery() 'This command will insert the data.
        MsgBox("Data Entered Successfully")
        OleDbConnection1.Close()
 
inserting data in access database

My application consists of 9 text boxes and one date time picker. I've to generate an automatic ID for students, as it is a Student management System, on click of new button then i've to insert that data into database. I need help as i'm really a novice in this field.
My code is:-

Me.OleDbInsertCommand1.CommandText = "INSERT INTO Enquiry(Enq_ID, Cand_Name, Cand_Addr, Cand_City, Cand_Cno, Cand_Mail," & _ " Cand_Qlf, Enq_Course, Cand_Ref, Enq_Date) VALUES (" & CInt(TextBox1.Text) & ",'" & TextBox2.Text & "','" & TextBox3.Text & "'," & CInt(TextBox4.Text) & ", '" & TextBox5.Text & "', '" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & DateTimePicker1.Value & "')" OleDbInsertCommand1 = New OleDbCommand(OleDbInsertCommand1.CommandText, OleDbConnection1) OleDbConnection1.Open() OleDbInsertCommand1.ExecuteNonQuery() 'This command will insert the data. OleDbConnection1.Close() MsgBox("Record entered successfully", MsgBoxStyle.Information, "Enquiry")
 
Aditya can edit the post and sort it out if Aditya is expecting any reasonable help.
 
Back
Top