开发者

Addition of the Dynamic records

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();
开发者_StackOverflow        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:

{
    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.Parameters.Add("@cmny_name", OleDbType.Char, 10) = txtCompanyName.Text;
    cmd.Parameters.Add("@cmny_location", OleDbType.Char, 30) = txtLocation.Text;
    cmd.Parameters.Add("@cmny_phn", OleDbType.Char, 10) = txtPhoneNumber.Text;
    cmd.ExecuteNonQuery(); 
    cmd.Dispose(); 
    con.Close();
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜