i want to add the records into the MS access database from the form which i have created, and display that records into the datagridview.. for inserting the records i have the following code,
{
OleDbConnection con = new OleDbConnection();
string constring = @"Provider = microsoft.jet.oledb.4.0;" + @"data source = c:\\Users\\logicwaves\\My Documents\\tbl_company.mdb";
con.ConnectionString = constring;
con.Open();
StringBuilder stb = new StringBuilder();
stb.Append("Insert into tbl_company(cmny_name, cmny_location,cmny_phn ) ");
stb.Append("Values('@cmny_name','@cmny_location','@cmny_phn')");
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = stb.ToString();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
but at “cmd.ExecuteNonQuery();” it is giving the error that “OleDbException was handled”…can any one tell me that how should i overcome from this problem?
I think you need something like this:
You need to actually pass the parameters into your query using the
cmd.Parameters.Add()method. Of course, in my example I am assuming you have some text box controls, but these inputs could be coming from anywhere. You get the point.