user383664
user383664

Reputation: 107

UPDATE STATEMENT in asp.net using c#

i am using asp.net with C# as code behind


   OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\CJP\My Documents\Visual Studio 2005\WebSites\NewElligibleSoft\elligiblity.mdb;Persist Security Info=False");
        cn.Open();
        string sql = "UPDATE main SET s_name='"+TextBox1.Text+"',inst_code='"+DropDownList1.SelectedItem+"',ms_oms='"+Label7.Text+"',elligiblity='"+Label12.Text+"',Board='"+DropDownList5.SelectedItem+"',percentage='"+TextBox4.Text+"' WHERE elg_id = '"+DropDownList4.SelectedItem+"'";
        OleDbCommand cmd = new OleDbCommand(sql, cn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        cn.Close();
        Response.Write("alert('DATA UPDATED')");

i am getting error on

cmd.ExecuteNonQuery();

that Data type mismatch in criteria expression.

Upvotes: 3

Views: 80493

Answers (5)

Zo Has
Zo Has

Reputation: 13038

Don't code like

string connection_string="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\CJP\My Documents\Visual Studio 2005\WebSites\NewElligibleSoft\elligiblity.mdb;Persist Security Info=False";
using(OleDbConnection cn = new OleDbConnection(connection_string))
{
     cn.Open();
     string sql = "UPDATE main SET s_name=?,inst_code=?,ms_oms=?,elligiblity=?,Board=?,percentage=?,amount=? WHERE elg_id =?";

     using(OleDbCommand cmd = new OleDbCommand(sql, cn))
     {
          cmd.Parameters.Add(new OleDbParameter("s_name",TextBox1.Text.Trim()));
          cmd.Parameters.Add(new OleDbParameter("inst_code",DropDownList1.SelectedItem.Value.ToString()));
          cmd.Parameters.Add(new OleDbParameter("ms_oms",Label7.Text.ToString()));
          cmd.Parameters.Add(new OleDbParameter("elligiblity",Label12.Text));
          cmd.Parameters.Add(new OleDbParameter("Board",DropDownList5.SelectedItem.Value.ToString()));
          cmd.Parameters.Add(new  OleDbParameter("percentage",DropDownList5.SelectedItem.Value.ToString()));
          cmd.Parameters.Add(new OleDbParameter(amount",DropDownList5.SelectedItem.Value.ToString()));
          cmd.Parameters.Add(new OleDbParameter("elg_id",DropDownList5.SelectedItem.Value.ToString()));
          cmd.ExecuteNonQuery();
          cn.Close();
     }
}        
 Response.Write("alert('DATA UPDATED')");

Upvotes: 4

Aakashi
Aakashi

Reputation: 41

OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\CJP\My Documents\Visual Studio 2005\WebSites\NewElligibleSoft\elligiblity.mdb;Persist Security Info=False");
        cn.Open();
        string sql = "UPDATE main SET s_name='" + TextBox1.Text + "',inst_code='" + DropDownList1.SelectedItem.Value.ToString() + "',ms_oms='" + Label7.Text + "',elligiblity='" + Label12.Text + "',Board='" + DropDownList5.SelectedItem.Value.ToString() + "',percentage='" + float.Parse(TextBox4.Text) + "',amount='" + Label10.Text + "' WHERE elg_id = " + DropDownList4.SelectedItem.Value + "";
        OleDbCommand cmd = new OleDbCommand(sql, cn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        cn.Close();
        Response.Write("alert('DATA UPDATED')");

Upvotes: 0

Chase Florell
Chase Florell

Reputation: 47437

Can you try DropDownList1.SelectedItem.Text or DropDownList1.SelectedItem.Value

This should be the same for all DropDownLists.

Also you might have to convert TextBox4 to the appropriate datatype for "percentage".

Assuming that the percentage is a Double, you'd need something like

Double.Parse(Textbox4.Text)

Lastly, if you're not sending a "string" to the query, you would be really good to remove the single quotes from those fields. That way you're not parsing the data but still sending string information.

Upvotes: 1

user383664
user383664

Reputation: 107

this is de correct code


OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\CJP\My Documents\Visual Studio 2005\WebSites\NewElligibleSoft\elligiblity.mdb;Persist Security Info=False");
        cn.Open();
        string sql = "UPDATE main SET s_name='" + TextBox1.Text + "',inst_code='" + DropDownList1.SelectedItem.Value.ToString() + "',ms_oms='" + Label7.Text + "',elligiblity='" + Label12.Text + "',Board='" + DropDownList5.SelectedItem.Value.ToString() + "',percentage='" + float.Parse(TextBox4.Text) + "',amount='" + Label10.Text + "' WHERE elg_id = " + DropDownList4.SelectedItem.Value + "";
        OleDbCommand cmd = new OleDbCommand(sql, cn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        cn.Close();
        Response.Write("alert('DATA UPDATED')");

thanxx

Upvotes: 0

Fyodor Soikin
Fyodor Soikin

Reputation: 80915

Remove single quotes around DropDownList4.SelectedItem. I bet your elg_id column is of type integer or something, and you're giving it a string.

Having that said, you would be really better off if you provided text of error, database table structure and maybe some other information so that people wouldn't have to read your mind.

Upvotes: 1

Related Questions