Click here to Skip to main content
15,901,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to save some records to store a table through front end.
here i am selecting product_name from dropdownlist there rate display as selected dropdownlist from database now inserting quantity fron text box and another text box calculate amount and then using save button I am getting error

system.formatexception: failed="" to="" convert="" parameter="" value="" from="" a="" string="" int32.="" input="" was="" not="" in="" correct="" format.="" at="" numberstyles="" numberformatinfo="" boolean="" type="" iformatprovider="" metatype="" ---="" end="" of="" inner="" exception="" stack="" trace="" sqlparametercollection="" runbehavior="" dbasyncresult="" eventargs="" 102=""

and my source code is..
C#
 protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
            SqlCommand cmd;
            SqlDataAdapter da=new SqlDataAdapter();
            DataSet ds=new DataSet();
            con.Open();
            try
            {
                cmd=new SqlCommand("insert into product(product_id,product_name,product_rate,product_quantity,product_total_amount)values(insert into product('@product_id',@product_name,@product_rate,@product_quantity,@product_total_amount)",con);
                cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = DropDownList1.SelectedItem.Text;
                cmd.Parameters.Add("@product_name", SqlDbType.NVarChar).Value = DropDownList1.Items.ToString();
                cmd.Parameters.Add("@product_quanity", SqlDbType.NVarChar).Value = txtqty.Text;
                cmd.Parameters.Add("@product_rate", SqlDbType.NVarChar).Value = LblRate1.TemplateControl;
                cmd.Parameters.Add("@product_total_amount", SqlDbType.NVarChar).Value = txtTotalAmount.Text;
                cmd.ExecuteNonQuery();
                Response.Write("yr records updated..");
                da = new SqlDataAdapter("select * from product",con);
                GridView1.DataSource = ds;
                GridView1.DataBind();
             }
            catch(Exception exe)
            {
                Response.Write(exe);
            }
           // cmd.Dispose();
            ds.Dispose();
            con.Close();            
}

what is the wrong input in this code. Please tell me?
Posted
Updated 12-Sep-10 22:56pm
v3

Error lies here:
C#
cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = DropDownList1.SelectedItem.Text;


You are trying to assign a string type of value to parameter defined as Int type.

Try:
C#
cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = Convert.ToInt32(DropDownList1.SelectedItem.Text);

Obviously assuming it can be converted!
 
Share this answer
 
try with this..

cmd=new SqlCommand("insert into product(product_id,product_name,product_rate,product_quantity,product_total_amount)values (@product_id,@product_name,@product_rate,@product_quantity,@product_total_amount)",con);
 
Share this answer
 
v3
Comments
call to .net 13-Sep-10 5:26am    
i have modified query but still same error coming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900