不会插入数据 [英] data would not be inserted
问题描述
即使没有错误,也不会在表格中插入数据。
even there is no error , data won't be inserted in the table.
sing System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private void ExecuteInsert(string Prodname, int Cat, bool Type, Int64 Price, int Stock)
{
SqlConnection obj = new SqlConnection(ConfigurationManager.ConnectionStrings["ZMDB"].ToString());
string sql = "INSERT INTO Products (Prodname, Cat,Type,Price,Stock) VALUES " + " (@Prodname,@Cat,@Type,@Price,@Stock)";
try
{
obj.Open();
SqlCommand cmd = new SqlCommand(sql, obj);
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@Prodname", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@Cat", SqlDbType.Int);
param[2] = new SqlParameter("@Type", SqlDbType.Binary);
param[3] = new SqlParameter("@Price", SqlDbType.BigInt);
param[3] = new SqlParameter("@Stock", SqlDbType.Int);
param[0].Value = Prodname;
param[1].Value = Cat;
param[2].Value = Type;
param[3].Value = Price;
param[4].Value = Stock;
cmd.Parameters.Add(param[0]);
cmd.Parameters.Add(param[1]);
cmd.Parameters.Add(param[2]);
cmd.Parameters.Add(param[3]);
cmd.Parameters.Add(param[4]);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
obj.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
ExecuteInsert(TextBox1.Text, Convert.ToInt32(DropDownList1.Text), Convert.ToBoolean(DropDownList2.Text), Convert.ToInt64(TextBox2.Text), Convert.ToInt32(TextBox3.Text));
Response.Write(" Your data is inserted successfully." );
}
}
推荐答案
我认为你应该检查以下代码 -
I think you should check the following code -
SqlParameter [] param = new SqlParameter [1];
param [0] = new SqlParameter(@ Prodname,SqlDbType.VarChar,50);
param [1] = new SqlParameter(@ Cat,SqlDbType.Int);
param [2] =新的SqlParameter(@ Type,SqlDbType.Binary);
param [3] = new SqlParameter(@ Price,SqlDbType.BigInt );
param [3] =新的SqlParameter(@ Stock,SqlDbType.Int);
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@Prodname", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@Cat", SqlDbType.Int);
param[2] = new SqlParameter("@Type", SqlDbType.Binary);
param[3] = new SqlParameter("@Price", SqlDbType.BigInt);
param[3] = new SqlParameter("@Stock", SqlDbType.Int);
1。你正在定义
1. You are defining
SqlParameter [] param = new SqlParameter [1];
SqlParameter[] param = new SqlParameter[1];
一个对象数组并使用param [0] ,param [1]等。这将为您提供索引超出范围的例外。
2.您可以查看您使用山姆的价格和股票e param [3]。
声明
a single object array and using param[0], param[1] etc. This will give you Index Out of bound exception.
2. You can see for Price and Stock you are using same param[3].
Declare
SqlParameter[] param = new SqlParameter[5];
,您将获得所需的结果。
谢谢。
and you will get the desired result.
Thanks.
你应该改变你的SqlConnection如下。
SqlConnection obj = new SqlConnection(ConfigurationManager.ConnectionStrings [ZMDB ] .ConnectionString);
You should change your SqlConnection as below.
SqlConnection obj = new SqlConnection(ConfigurationManager.ConnectionStrings["ZMDB"].ConnectionString);
感谢Jitendra和Ashutosh的解决方案;)
Thanks to Jitendra and Ashutosh for your solutions ;)
这篇关于不会插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!