我想在单击“提交”按钮后逐行插入所有项目,但是不知何故我的变量“ @DrinkName”相同并且出现错误。请查看我的代码和屏幕截图,以便更好地理解。
Category.aspx.cs
protected void BtnSubmit_Click(object sender, EventArgs e)
{
string connString = "sql database";
string insertCommand = "INSERT INTO tbDrinks ( DrinkName, DateOfOrder, Qty, UserName, UserCompany) " +
"values(@DrinkName, @DateOfOrder, @Qty, @UserName, @UserCompany)"; //for testing purpose
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCommand, conn))
{
sqlcmd.Parameters.AddWithValue("@DrinkName", lblCoffee.Text);
sqlcmd.Parameters.AddWithValue("@DrinkName", lblEnglishTea.Text);
sqlcmd.Parameters.AddWithValue("@DateOfOrder", DateTime.Today);
sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
sqlcmd.Parameters.AddWithValue("@Qty", ddlEnglishTea.SelectedValue);
sqlcmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
sqlcmd.Parameters.AddWithValue("@UserCompany", txtCompanyName.Text);
sqlcmd.ExecuteNonQuery();
}
}
}
这是我创建的Category.aspx
,其中每个项目都有不同的下拉列表。
<table class="auto-style6">
<tr>
<td class="auto-style9">
<strong>
<asp:Label ID="lblCompany" runat="server" Text="Company Name : "></asp:Label>
</strong>
</td>
<td class="auto-style10">
<strong>
<asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox>
</strong>
</td>
</tr>
</table>
<table id="tbHotDrinks" class="auto-style3" border="1" bordercolor="#1FC3F3" runat="server">
<tr>
<td>
<asp:Label ID="lblCoffee" runat="server" Text="Coffee"></asp:Label>
</td>
<td class="auto-style2">
<asp:Image ID="coffee" runat="server" Height="76px" ImageUrl="~/images/coffee.gif" Width="99px" ImageAlign="TextTop" />
</td>
<td class="auto-style11">
<asp:DropDownList ID="ddlCoffee" runat="server">
<asp:ListItem>Qty</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblEnglishTea" runat="server" Text="English Tea"></asp:Label>
</td>
<td class="auto-style2">
<asp:Image ID="Image1" runat="server" Height="76px" ImageUrl="~/images/EnglishTea.gif" Width="99px" ImageAlign="TextTop" />
</td>
<td class="auto-style11">
<asp:DropDownList ID="ddlEnglishTea" runat="server">
<asp:ListItem>Qty</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<asp:Button ID="BtnSubmit" runat="server" OnClick="BtnSubmit_Click" Text="Submit" />
下面是我想逐行添加的示例数据库屏幕快照。
我在Category.aspx.cs
中尝试过的代码如下。不知何故,它仍然不正确。我只想要最简单的方法。
protected void BtnSubmit_Click(object sender, EventArgs e)
{
string connString = "Data Source";
string insertCommand = "INSERT INTO tbDrinks ( DrinkName, DateOfOrder, Qty, UserName, UserCompany) " +
"VALUES (@DrinkName, @DateOfOrder, @Qty, @UserName, @UserCompany)";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCommand, conn))
{
sqlcmd.Parameters.AddWithValue("@DrinkName", lblCoffee.Text);
// sqlcmd.Parameters.AddWithValue("@DrinkName", lblEnglishTea.Text);
sqlcmd.Parameters.AddWithValue("@DateOfOrder", DateTime.Today);
sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
// sqlcmd.Parameters.AddWithValue("@Qty", ddlEnglishTea.SelectedValue);
sqlcmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
sqlcmd.Parameters.AddWithValue("@UserCompany", txtCompanyName.Text);
sqlcmd.ExecuteNonQuery();
}
using (SqlCommand sqlcmd1 = new SqlCommand(insertCommand, conn))
{
// sqlcmd.Parameters.AddWithValue("@DrinkName", lblCoffee.Text);
sqlcmd1.Parameters.AddWithValue("@DrinkName", lblEnglishTea.Text);
sqlcmd1.Parameters.AddWithValue("@DateOfOrder", DateTime.Today);
// sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
sqlcmd1.Parameters.AddWithValue("@Qty", ddlEnglishTea.SelectedValue);
sqlcmd1.Parameters.AddWithValue("@UserName", txtUserName.Text);
sqlcmd1.Parameters.AddWithValue("@UserCompany", txtCompanyName.Text);
sqlcmd1.ExecuteNonQuery();
}
}
}
答案 0 :(得分:2)
出现错误是因为您两次用相同的名称声明了Sql参数:
sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
sqlcmd.Parameters.AddWithValue("@DrinkName", lblEnglishTea.Text);
尝试如下:
protected void BtnSubmit_Click(object sender, EventArgs e)
{
string connString = "sql database";
string insertCommand = "INSERT INTO tbDrinks ( DrinkName, DateOfOrder, Qty, UserName, UserCompany) " +
"values(@DrinkName, @DateOfOrder, @Qty, @UserName, @UserCompany)"; //for testing purpose
using(SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using(SqlCommand sqlcmd = new SqlCommand(insertCommand, conn)) {
sqlcmd.Parameters.AddWithValue("@DrinkName", lblCoffee.Text);
// sqlcmd.Parameters.AddWithValue("@DrinkName", lblEnglishTea.Text);
sqlcmd.Parameters.AddWithValue("@DateOfOrder", DateTime.Today);
sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
// sqlcmd.Parameters.AddWithValue("@Qty", ddlEnglishTea.SelectedValue);
sqlcmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
sqlcmd.Parameters.AddWithValue("@UserCompany", txtCompanyName.Text);
sqlcmd.ExecuteNonQuery();
}
}
}
答案 1 :(得分:1)
首先,您要两次添加相同的参数。这需要通过更新该特定参数的值来纠正。
第二,您无需创建两个SqlCommand实例,而只需更新第二次插入的parameter的值,并使用更新后的值第二次运行ExecuteNonQuery()。您可以这样:
protected void BtnSubmit_Click (object sender, EventArgs e) {
string connString = "Data Source";
string insertCommand = "INSERT INTO tbDrinks ( DrinkName, DateOfOrder, Qty, UserName, UserCompany) " +
"values(@DrinkName, @DateOfOrder, @Qty, @UserName, @UserCompany)";
using (SqlConnection conn = new SqlConnection (connString)) {
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand (insertCommand, conn)) {
sqlcmd.Parameters.AddWithValue("@DrinkName", lblCoffee.Text);
sqlcmd.Parameters.AddWithValue("@DateOfOrder", DateTime.Today);
sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
sqlcmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
sqlcmd.Parameters.AddWithValue("@UserCompany", txtCompanyName.Text);
sqlcmd.ExecuteNonQuery();
// update the values and insert with updated values.
sqlcmd.Parameters["@DrinkName"].Value = lblEnglishTea.Text;
sqlcmd.Parameters["@Qty"].Value = ddlEnglishTea.SelectedValue;
sqlcmd.ExecuteNonQuery();
}
}
}
答案 2 :(得分:1)
如果您想简化代码以节省两次购买并使其易于维护,
创建一个类,该类将根据给定的参数插入一条记录,并重用该类以添加多行。
C#是面向对象的语言,因此我们可以从中受益。
// Represents data which can be different for different drinks
public class DrinkSelection
{
public string Name { get; set; }
public int Qty { get; set; }
}
public class DrinkPurchase
{
private readonly string _userName;
private readonly string _userCompany;
public DrinkPurchase(string userName, string userCompany)
{
_userName = userName;
_userCompany = userCompany;
}
public void Save(DrinkSelection drink)
{
var insert = @"
INSERT INTO tbDrinks (DrinkName, DateOfOrder, Qty, UserName, UserCompany)
VALUES (@DrinkName, @DateOfOrder, @Qty, @UserName, @UserCompany)
";
var parameters = new[]
{
new SqlParameter("@DrinkName", SqlDbType.Varchar) { Value = drink.Name },
new SqlParameter("@DateOfOrder", SqlDbType.DateTime) { Value = DateTime.Today },
new SqlParameter("@Qty", SqlDbType.Varchar) { Value = drink.Qty },
new SqlParameter("@UserName", SqlDbType.Varchar) { Value = _userName },
new SqlParameter("@UserCompany", SqlDbType.Varchar) { Value = _userCompany }
};
using (var connection = new SqlConnection(connectionString))
using (var command = connection.CreateCommand())
{
command.CommandText = insert;
command.Parameters.AddRange(parameters);
connection.Open();
command.ExecuteNonQuery();
}
}
}
用法
protected void BtnSubmit_Click(object sender, EventArgs e)
{
var purchase = new DrinkPurchase(txtUserName.Text, txtCompanyName.Text);
var coffee = new DrinkSelection
{
Name = lblCoffee.Text,
Qty = ddlCoffee.SelectedValue
};
var tea = new DrinkSelection
{
Name = lblEnglishTea.Text,
Qty = ddlEnglishTea.SelectedValue
};
purchase.Save(coffee);
purchase.Save(tea);
}
通过将逻辑提取到专用类中,您可以从对Web框架的依赖中收取业务逻辑费用。