English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

'This are my codes. I am trying to Insert data into my database(MS ACCESS).

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

con.Open()
Dim cmd As New OleDbCommand(strcon)

Dim strSQl As String = _
"INSERT into Tutor Values('" + _
tbemail.Text + "','" + _
tbpassword.Text + "','" + _
tbname.Text + "','" + _
rbgender.SelectedValue + "','" + _
tbcontactno.Text + "','" + _
tbaddress.Text + "','" + _
tbcountry.Text + "','" + _
tbDOB.Text + "','" + _
tbnric.Text + "','" + _
tbpostalcode.Text + "')"

cmd.CommandText = strSQl
cmd.ExecuteNonQuery()
Response.Redirect("confirmation.aspx")

End Sub
End Class

2006-07-04 09:03:15 · 2 answers · asked by cherylgis 1 in Computers & Internet Programming & Design

2 answers

Your query is not correct

Should be :

INSERT into tutor(fieldnames) VALUES (values).

Also write out your query string first before opening connection to database.

do some thing like this:

//button click
Dim strSQL as string = //your query string
con.open()
Dim cmd as new Oledbcommand(strcon,strSQL)
cmd.ExecuteNonQuery
con.close()
//....rest of the code

Also make sure that your connection string is defined globally on the page otherwise the button click will not find the connection string as it will fall out of scope.

Always close the db connection before redirecting to any page other wise your application will have problems.

If you're only learning vb.net then this kind of coding should be OK. Other wise never create/use queries like that. Instead try using stored procedures.

Hope this helps

2006-07-04 10:59:21 · answer #1 · answered by Anonymous · 1 0

You have to init the connection string

Con = new OleDbConnection(strcon)
Dim cmd as new OleDbCommand()
Dim strSQl As String = _
"INSERT into Tutor Values('" + _
tbemail.Text + "','" + _
tbpassword.Text + "','" + _
tbname.Text + "','" + _
rbgender.SelectedValue + "','" + _
tbcontactno.Text + "','" + _
tbaddress.Text + "','" + _
tbcountry.Text + "','" + _
tbDOB.Text + "','" + _
tbnric.Text + "','" + _
tbpostalcode.Text + "')"

cmd.Connection = con
cmd.CommandText = strSQl
con.Open()
cmd.ExecuteNonQuery()
con.Close()
con.Dispose()
Response.Redirect("confirmatio...


The connection string has to be known to the Connection object. Your code never makes it known, which is why you get the error.

2006-07-04 20:57:56 · answer #2 · answered by evolver 6 · 1 0

fedest.com, questions and answers