Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未从文本字段将数据插入数据库_C#_Sql_Database_Tsql - Fatal编程技术网

C# 未从文本字段将数据插入数据库

C# 未从文本字段将数据插入数据库,c#,sql,database,tsql,C#,Sql,Database,Tsql,因此,我尝试使用asp.net web窗体制作一个库存管理web应用程序, 我花了大约三个小时才把这个代码连接到数据库上, 这样做之后,我的web应用程序中的文本输入字段中的文本似乎没有被传递到数据库,我目前不知道是什么阻止了它 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString); protected void Pa

因此,我尝试使用asp.net web窗体制作一个库存管理web应用程序, 我花了大约三个小时才把这个代码连接到数据库上, 这样做之后,我的web应用程序中的文本输入字段中的文本似乎没有被传递到数据库,我目前不知道是什么阻止了它

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”命令来更改它
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;
}