【问题标题】:Data Not being inserted into Database from Text Fields数据未从文本字段插入数据库
【发布时间】:2018-09-23 21:44:44
【问题描述】:

所以我正在尝试使用 asp.net 网络表单制作一个库存管理网络应用程序, 我已经奋斗了大约三个小时才能让这段代码与数据库连接, 这样做后,现在我的网络应用程序中的文本输入字段中的文本似乎没有传递到数据库,我目前不知道是什么阻止了它。

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //'" + ItemTextBox.Text + "','" + BrandTextBox.Text + "','" + CostTextBox.Text + "','" + PriceTextBox.Text + "'" + ColourTextBox.Text + "'" + SizeTextBox.Text + "'" + QuantityTextBox.Text + ")", con
        con.Open();
        string sqlquery = "insert into [Project_X_Or] (Item,Brand,Cost,Price,Colour,Size,Quantity) values (@Item,@Brand,@Cost,@Price,@Colour,@Size,@Quantity)";
        SqlCommand command = new SqlCommand(sqlquery, con);
        command.ExecuteNonQuery();

        //Item Name**********
        string Item = ItemTextBox.Text;
        command.Parameters.AddWithValue("Item", Item);
        //Brand Of Product**********
        string Brand = BrandTextBox.Text;
        command.Parameters.AddWithValue("Brand", Brand);
        //Cost Of Item From Supplier**********
        string Cost = CostTextBox.Text;
        command.Parameters.AddWithValue("Cost", Cost);
        //Price Of Item When Sold With Design**********
        string Price = PriceTextBox.Text;
        command.Parameters.AddWithValue("Price", Price);
        //Colour Of Product**********
        string Colour = ColourTextBox.Text;
        command.Parameters.AddWithValue("Colour", Colour);
        //Size of Product (T-Shirts, Hoodies)**********
        string Size = SizeTextBox.Text;
        command.Parameters.AddWithValue("Size", Size);
        //Quantity Of Product In Stock**********
        string Quantity = QuantityTextBox.Text;
        command.Parameters.AddWithValue("Quantity", Quantity);
        con.Close();


        string result = "Added  " + Item + " " + Brand + " " + Cost + " " + Cost + " " + Price + " " + Colour + " " + Size + " " + Quantity;

        resultLabel.Text = result;

        if (string.IsNullOrEmpty(ItemTextBox.Text))
        {
            resultLabel.Text=("Enter Item Into Item Field");
        }

        if (string.IsNullOrEmpty(QuantityTextBox.Text))
        {
            resultLabel.Text = ("Enter Amount Into Quantity Field");
        }
        //if (string.IsNullOrEmpty(""))
        //    {
        //    resultLabel2.Text = ("Please Fill Out Fields Above");
        //}


        ItemTextBox.Text = string.Empty;
        BrandTextBox.Text = string.Empty;
        CostTextBox.Text = string.Empty;
        PriceTextBox.Text = string.Empty;
        ColourTextBox.Text = string.Empty;
        SizeTextBox.Text = string.Empty;
        QuantityTextBox.Text = string.Empty;
    }

【问题讨论】:

  • 您在分配参数值之前正在执行命令,因此它在没有参数的情况下运行。将command.ExecuteNonQuery() 移动到添加参数之后,但在con.Close() 之前
  • 哦,该死的,一定是累了,我不知道为什么我没看到。
  • 现在遇到此错误字符串或二进制数据将被截断。声明已终止。
  • 忽略上面我的列被设置为char (10) alter命令来改变它

标签: c# sql database tsql


【解决方案1】:

你应该在添加Paramter后Execute

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    //'" + ItemTextBox.Text + "','" + BrandTextBox.Text + "','" + CostTextBox.Text + "','" + PriceTextBox.Text + "'" + ColourTextBox.Text + "'" + SizeTextBox.Text + "'" + QuantityTextBox.Text + ")", con
    con.Open();
    string sqlquery = "insert into [Project_X_Or] (Item,Brand,Cost,Price,Colour,Size,Quantity) values (@Item,@Brand,@Cost,@Price,@Colour,@Size,@Quantity)";
    SqlCommand command = new SqlCommand(sqlquery, con);

    //Item Name**********
    string Item = ItemTextBox.Text;
    command.Parameters.AddWithValue("Item", Item);
    //Brand Of Product**********
    string Brand = BrandTextBox.Text;
    command.Parameters.AddWithValue("Brand", Brand);
    //Cost Of Item From Supplier**********
    string Cost = CostTextBox.Text;
    command.Parameters.AddWithValue("Cost", Cost);
    //Price Of Item When Sold With Design**********
    string Price = PriceTextBox.Text;
    command.Parameters.AddWithValue("Price", Price);
    //Colour Of Product**********
    string Colour = ColourTextBox.Text;
    command.Parameters.AddWithValue("Colour", Colour);
    //Size of Product (T-Shirts, Hoodies)**********
    string Size = SizeTextBox.Text;
    command.Parameters.AddWithValue("Size", Size);
    //Quantity Of Product In Stock**********
    string Quantity = QuantityTextBox.Text;
    command.Parameters.AddWithValue("Quantity", Quantity);
    command.ExecuteNonQuery();
    con.Close();


    string result = "Added  " + Item + " " + Brand + " " + Cost + " " + Cost + " " + Price + " " + Colour + " " + Size + " " + Quantity;

    resultLabel.Text = result;

    if (string.IsNullOrEmpty(ItemTextBox.Text))
    {
        resultLabel.Text=("Enter Item Into Item Field");
    }

    if (string.IsNullOrEmpty(QuantityTextBox.Text))
    {
        resultLabel.Text = ("Enter Amount Into Quantity Field");
    }
    //if (string.IsNullOrEmpty(""))
    //    {
    //    resultLabel2.Text = ("Please Fill Out Fields Above");
    //}


    ItemTextBox.Text = string.Empty;
    BrandTextBox.Text = string.Empty;
    CostTextBox.Text = string.Empty;
    PriceTextBox.Text = string.Empty;
    ColourTextBox.Text = string.Empty;
    SizeTextBox.Text = string.Empty;
    QuantityTextBox.Text = string.Empty;
}

【讨论】:

    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 2015-03-01
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多