通过asp.net在数据库中使用存储数据 [英] Using store data in database with asp.net
问题描述
我正在尝试保存一些记录以通过前端存储表.
在这里,我从下拉列表中选择product_name,从数据库中将速率显示为选定的下拉列表,现在插入数量fron文本框,另一个文本框计算数量,然后使用保存按钮,我得到错误
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 ="
我的源代码是..
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..
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?
推荐答案
错误在这里:
Error lies here:
cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = DropDownList1.SelectedItem.Text;
您正在尝试将值的字符串类型分配给定义为Int类型的参数.
试试:
You are trying to assign a string type of value to parameter defined as Int type.
Try:
cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = Convert.ToInt32(DropDownList1.SelectedItem.Text);
显然假设它可以转换!
Obviously assuming it can be converted!
尝试一下..
cmd = new SqlCommand(插入到product(product_id,product_name,product_rate,product_quantity,product_total_amount)值(@ product_id,@ product_name,@ product_rate,@ product_quantity,@ product_total_amount)中,con);
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);
这篇关于通过asp.net在数据库中使用存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!