I would like to insert all the items row by row after clicking Submit button but somehow my variable '@DrinkName' is same and got errors. Kindly see my code and screenshot for better understanding.
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();
}
}
}
This is Category.aspx which I created with different dropdown list for each item.
<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" />
Below here is my example database screenshot that I would like to add row by row.
The code I have tried in Category.aspx.cs is below. Somehow it's still not correct yet. I just want simplest way to do it.
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();
}
}
}

sqlcmd.Parameters.Addmethod. Then assign values to parameters usingsqlcmd.Parameters["@DrinkName"].Value = lblCoffee.Text. Then execute the command. Then again set the parameter values and again execute the command. This way you don't have to create two SqlCommand. Only one SqlCommand object needed.