我在ASP.NET中创建一个网站(在c#中),其中的广告系列列在一个数据列表中(通过在数据列表中使用<div>
相互分离,因此每个广告系列都列在一个块中)
我无法通过c#更新“广告系列”表格中的特定列,因为它无法找到标量变量@camp_id(广告系列的ID)。
我使用此命令更新:
sqlCmd = "UPDATE Campagnes SET camp_status=2 WHERE camp_id=@camp_id";
有人知道如何更新“camp_status&#39;使用&#39; camp_id&#39;所以只有&#39; camp_status&#39;特定广告系列(而不是数据列表中的其他广告系列)会更新吗?
编辑:这是我使用的完整代码:
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string sqlConn;
string sqlCmd;
sqlConn = @"Data Source=my-ip,1433;Initial Catalog=DbName;
Integrated Security=False;user id=sa;password=password";
sqlCmd= "UPDATE Campagnes SET camp_status=2 WHERE camp_id=@camp_id";
conn.ConnectionString = sqlConn;
cmd.Connection = conn;
cmd.CommandText = sqlCmd;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
因为你可以看到我还没有使用参数,因为我不知道如何添加它以及它如何从数据库中读取值。
我目前正在使用的完整代码:
SqlConnection conn2 = new SqlConnection();
SqlCommand cmd2 = new SqlCommand();
string sqlConn2;
string sqlCmd2;
sqlConn2 = @"Data Source=my-ip,1433;Initial Catalog=dbname;Integrated Security=False;user id=sa;password=password";
sqlCmd2 = "select * from Campagnes";
conn2.ConnectionString = sqlConn2;
cmd2.Connection = conn2;
cmd2.CommandText = sqlCmd2;
conn2.Open();
SqlDataReader dr = cmd2.ExecuteReader();
while (dr.Read())
{
id = dr.GetInt32(0);
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string sqlConn;
string sqlCmd;
sqlConn = @"Data Source=my-ip,1433;Initial Catalog=dbname;Integrated Security=False;user id=sa;password=password";
sqlCmd = "UPDATE Campagnes SET camp_status=1 WHERE camp_id=" + id;
cmd.Parameters.AddWithValue("@camp_id", id);
conn.ConnectionString = sqlConn;
cmd.Connection = conn;
cmd.CommandText = sqlCmd;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
无法在此处上传我的图片,因此这里是屏幕截图的外部链接:http://i.imgur.com/P8MeKm4.png
正如您在图片中看到的,所有分隔的白色块都是一个数据列表,但是在添加新广告系列时将生成一个div。当我们点击右下方的编辑按钮时,camp_status需要设置为2(因此管理员知道需要编辑广告系列)。
ASP来源:
div id="popUpPanel">
<p>Waarom vindt u dat deze campagne nog niet in orde is? Geef uw feedback hieronder in:</p>
<asp:TextBox ID="TextBox1" CssClass="box" TextMode="MultiLine" runat="server"></asp:TextBox>
<asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="OK" />
</div>
<asp:DataList CellPading="5" ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" style="margin-right: 0px" >
<ItemTemplate>
<div class="list" style="padding-left: 25px; padding-right: 10px; padding-top: 10px;">
<asp:Label ID="titelLabel" runat="server" style="font-size: xx-large" Text='<%# Eval("titel") %>' />
<br />
<asp:Label ID="Label1" runat="server" style="font-size: xx-large; display: none;" Text='<%# Eval("camp_id") %>' />
<asp:Label ID="datum_geplaatstLabel" runat="server" Text='<%# Eval("datum_geplaatst") %>' />
<br /><br />
<strong>Korte beschrijving:</strong><br />
<asp:Label ID="korte_beschrijvingLabel" runat="server" Text='<%# Eval("korte_beschrijving") %>' />
<br /><br />
<strong>Lange beschrijving:</strong><br />
<asp:Label ID="lange_beschrijvingLabel" runat="server" Text='<%# Eval("lange_beschrijving") %>' />
<br />
<table class="auto-style1">
<tr>
<td class="auto-style2"><strong>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/img/edit.png" OnClientClick="showPopUp(); return false;" Style="margin-left:9px;" />
</strong></td>
<td><strong>
<asp:ImageButton ID="ImageButton1" runat="server" asp:Imagebutt="" ImageUrl="~/img/vink.png" Style="margin-left:0px;" />
</strong></td>
</tr>
</table>
<br />
</div>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WebhoostConnectionString %>" SelectCommand="SELECT [titel], [datum_geplaatst], [korte_beschrijving], [lange_beschrijving], [camp_id] FROM [Campagnes]"></asp:SqlDataSource>
So when we click on the 'Button1' in the PopupPanel the camp_status needs to be set to 2 only of that specific campaign. As you can see in the source I was also trying to use the label I was talking about, but everytime a new div is created, all coming labels will have 'Label1' as ID so it will also pick all campaign ID's and not just one..
ASP源代码(使用按钮中的CommandArgument):
<div id="popUpPanel">
<p>Waarom vindt u dat deze campagne nog niet in orde is? Geef uw feedback hieronder in:</p>
<asp:TextBox ID="TextBox1" CssClass="box" TextMode="MultiLine" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" CommandArgument='<%= Campagnes.camp_id %>' />
</div>
<asp:DataList CellPading="5" ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" style="margin-right: 0px" >
<ItemTemplate>
<div class="list" style="padding-left: 25px; padding-right: 10px; padding-top: 10px;">
<asp:Label ID="titelLabel" runat="server" style="font-size: xx-large" Text='<%# Eval("titel") %>' />
<br />
<asp:Label ID="Label1" runat="server" style="font-size: xx-large; display: none;" Text='<%# Eval("camp_id") %>' />
<asp:Label ID="datum_geplaatstLabel" runat="server" Text='<%# Eval("datum_geplaatst") %>' />
<br /><br />
<strong>Korte beschrijving:</strong><br />
<asp:Label ID="korte_beschrijvingLabel" runat="server" Text='<%# Eval("korte_beschrijving") %>' />
<br /><br />
<strong>Lange beschrijving:</strong><br />
<asp:Label ID="lange_beschrijvingLabel" runat="server" Text='<%# Eval("lange_beschrijving") %>' />
<br />
<table class="auto-style1">
<tr>
<td class="auto-style2"><strong>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/img/edit.png" OnClientClick="showPopUp(); return false;" Style="margin-left:9px;" />
</strong></td>
<td><strong>
<asp:ImageButton ID="ImageButton1" runat="server" asp:Imagebutt="" ImageUrl="~/img/vink.png" Style="margin-left:0px;" />
</strong></td>
</tr>
</table>
<br />
</div>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WebhoostConnectionString %>" SelectCommand="SELECT [titel], [datum_geplaatst], [korte_beschrijving], [lange_beschrijving], [camp_id] FROM [Campagnes]"></asp:SqlDataSource>
C#源代码(编辑EventArgs到CommandEventArgs e):
string id;
SqlConnection conn2 = new SqlConnection();
SqlCommand cmd2 = new SqlCommand();
string sqlConn2;
string sqlCmd2;
sqlConn2 = @"Data Source=81.169.242.73,1433;Initial Catalog=Webhoost;Integrated Security=False;user id=sa;password=63310Kw1c";
sqlCmd2 = "select * from Campagnes";
conn2.ConnectionString = sqlConn2;
cmd2.Connection = conn2;
cmd2.CommandText = sqlCmd2;
conn2.Open();
SqlDataReader dr = cmd2.ExecuteReader();
while (dr.Read())
{
id = e.CommandArgument.ToString();
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string sqlConn;
string sqlCmd;
sqlConn = @"Data Source=81.169.242.73,1433;Initial Catalog=Webhoost;Integrated Security=False;user id=sa;password=63310Kw1c";
sqlCmd = "UPDATE Campagnes SET camp_status=1 WHERE camp_id=" + id;
cmd.Parameters.AddWithValue("@camp_id", id);
conn.ConnectionString = sqlConn;
cmd.Connection = conn;
cmd.CommandText = sqlCmd;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
答案 0 :(得分:3)
您必须定义并添加参数@camp_id
text-decoration: underline;