APPLICATION UPDATE in asp.net using C #

I am using asp.net with c # as 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+"',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 get an error

cmd.ExecuteNonQuery();

that the data type mismatch in the criteria expression.

+3
source share
5 answers

Do not use code

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')");
+4
source

Can you try DropDownList1.SelectedItem.TextorDropDownList1.SelectedItem.Value

This should be the same for all DropDownLists.

You may also need to convert TextBox4 to the appropriate data type for "percent".

Assuming the percentage is Double, you need something like

Double.Parse(Textbox4.Text)

, "" , . , , .

+1

Remove single quotes around DropDownList4.SelectedItem. I am sure that your column elg_idis of type integer or something else, and you give it a row.

Having said that, it would be better for you to provide the text of the error, the structure of the database table, and possibly some other information so that people cannot read your mind.

+1
source

this is the correct code

 
OleDbConnection cn = new OleDbConnection (@ "Provider = Microsoft.Jet.OLEDB.4.0;   = D:\Documents and Settings\CJP\  \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() + "', percent = '" + float.Parse(TextBox4.Text) + "', amount = '" + Label10.Text + "'WHERE elg_id =" + DropDownList4.SelectedItem.Value + ";       OleDbCommand cmd =  OleDbCommand (sql, cn);       cmd.ExecuteNonQuery();       cmd.Dispose();       cn.Close();       Response.Write(" alert ('DATA UPDATED')");

thanxx
0
source
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')");
0
source

Source: https://habr.com/ru/post/1753981/


All Articles