这是一个令人头疼的问题(对我而言,至少)。
我有一个GridView,用户可以在页脚中输入很多数据字段的信息。一般来说,他们遵循以下格式:
<asp:TemplateField HeaderText="Date" ItemStyle-HorizontalAlign="Center">
<FooterTemplate>
<asp:TextBox ID="NewDate" runat="server" Width="25px" Height="10px" Font-Size="Smaller" ValidateRequestMode="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="NewDate" Display="Dynamic" ErrorMessage="You must enter a valid date (MM/DD/YYYY) before submitting...">*</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("WeekOf","{0:MM/dd}") %>'></asp:Label>
</ItemTemplate>
InsertCommand看起来像这样(一般化):
InsertCommand="INSERT INTO [Table] (WeekOf,User,OtherData)
VALUES (Convert(Date,@WeekOf,101), @User, @OtherData)">
@User由InsertParameter控制哪个工作正常(我知道这是因为它与我的函数SelectCommand / SelectParameter完全相同)。
无论如何,由于这些字段位于gridview中,我无法为它们分配静态InsertParameters,因为它们位于GridView的页脚中,这会随着行数的变化而变化(我知道...对如何进行任何启示这样做会很棒)所以我试图以编程方式声明像this这样的变量,但是我得到一个“必须声明标量变量”@WeekOf“错误。这是我的GridView_RowCommand函数的一般概念:< / p>
TextBox WeekOfin = SalesReportingGridView.FooterRow.FindControl("NewDate") as TextBox;
TextBox OtherDatain = SalesReportingGridView.FooterRow.FindControl("NewOtherData") as TextBox;
SqlParameter WeekOf = new SqlParameter("@WeekOf", SqlDbType.VarChar, 30);
WeekOf.Direction = ParameterDirection.Input;
WeekOf.Value = WeekOfin.Text;
insertParameters.Add(WeekOf);
SqlParameter OtherData = new SqlParameter("@OtherData", SqlDbType.Int);
OtherData.Direction = ParameterDirection.Input;
OtherData.Value = OtherDatain.Text;
insertParameters.Add(OtherData);
SqlDataSource2.Insert();
这是完整的相关代码 -
private List<SqlParameter> insertParameters = new List<SqlParameter>();
protected void SqlDataSource2_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters.Clear();
foreach (SqlParameter p in insertParameters)
e.Command.Parameters.Add(p);
}
protected void SalesReportingGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert" && Page.IsValid)
{
TextBox WeekOfin = SalesReportingGridView.FooterRow.FindControl("NewDate") as TextBox;
TextBox Data1in = SalesReportingGridView.FooterRow.FindControl("NewData1") as TextBox;
TextBox Data2in = SalesReportingGridView.FooterRow.FindControl("NewData2") as TextBox;
TextBox Data3in = SalesReportingGridView.FooterRow.FindControl("NewData3") as TextBox;
TextBox Data4in = SalesReportingGridView.FooterRow.FindControl("NewData4") as TextBox;
TextBox Data5in = SalesReportingGridView.FooterRow.FindControl("NewData5") as TextBox;
TextBox Data6in = SalesReportingGridView.FooterRow.FindControl("NewData6") as TextBox;
TextBox Data7in = SalesReportingGridView.FooterRow.FindControl("NewData7") as TextBox;
TextBox Data8in = SalesReportingGridView.FooterRow.FindControl("NewData8") as TextBox;
TextBox Data9in = SalesReportingGridView.FooterRow.FindControl("NewData9") as TextBox;
TextBox Data10in = SalesReportingGridView.FooterRow.FindControl("NewData10") as TextBox;
TextBox Data11in = SalesReportingGridView.FooterRow.FindControl("NewData11") as TextBox;
TextBox Data12in = SalesReportingGridView.FooterRow.FindControl("NewData12") as TextBox;
TextBox Data13in = SalesReportingGridView.FooterRow.FindControl("NewData13") as TextBox;
TextBox Data14in = SalesReportingGridView.FooterRow.FindControl("NewData14") as TextBox;
TextBox Data15in = SalesReportingGridView.FooterRow.FindControl("NewData15") as TextBox;
TextBox Data16in = SalesReportingGridView.FooterRow.FindControl("NewData16") as TextBox;
TextBox Data17in = SalesReportingGridView.FooterRow.FindControl("NewData17") as TextBox;
TextBox Data18in = SalesReportingGridView.FooterRow.FindControl("NewData18") as TextBox;
TextBox Data19in = SalesReportingGridView.FooterRow.FindControl("NewData19") as TextBox;
TextBox Data20in = SalesReportingGridView.FooterRow.FindControl("NewData20") as TextBox;
TextBox Data21in = SalesReportingGridView.FooterRow.FindControl("NewData21") as TextBox;
SqlParameter WeekOf = new SqlParameter("@WeekOf", SqlDbType.VarChar, 30);
WeekOf.Direction = ParameterDirection.Input;
WeekOf.Value = WeekOfin.Text;
insertParameters.Add(WeekOf);
SqlParameter Data1 = new SqlParameter("@Data1", SqlDbType.Int);
Data1.Direction = ParameterDirection.Input;
Data1.Value = Data1in.Text;
insertParameters.Add(Data1);
SqlParameter Data2 = new SqlParameter("@Data2", SqlDbType.Int);
Data2.Direction = ParameterDirection.Input;
Data2.Value = Data2in.Text;
insertParameters.Add(Data2);
SqlParameter Data3 = new SqlParameter("@Data3", SqlDbType.Int);
Data3.Direction = ParameterDirection.Input;
Data3.Value = Data3in.Text;
insertParameters.Add(Data3);
SqlParameter Data4 = new SqlParameter("@Data4", SqlDbType.Int);
Data4.Direction = ParameterDirection.Input;
Data4.Value = Data4in.Text;
insertParameters.Add(Data4);
SqlParameter Data5 = new SqlParameter("@Data5", SqlDbType.Int);
Data5.Direction = ParameterDirection.Input;
Data5.Value = Data5in.Text;
insertParameters.Add(Data5);
SqlParameter Data6 = new SqlParameter("@Data6", SqlDbType.Int);
Data6.Direction = ParameterDirection.Input;
Data6.Value = Data6in.Text;
insertParameters.Add(Data6);
SqlParameter Data7 = new SqlParameter("@Data7", SqlDbType.Int);
Data7.Direction = ParameterDirection.Input;
Data7.Value = Data7in.Text;
insertParameters.Add(Data7);
SqlParameter Data8 = new SqlParameter("@Data8", SqlDbType.Int);
Data8.Direction = ParameterDirection.Input;
Data8.Value = Data8in.Text;
insertParameters.Add(Data8);
SqlParameter Data9 = new SqlParameter("@Data9", SqlDbType.Int);
Data9.Direction = ParameterDirection.Input;
Data9.Value = Data9in.Text;
insertParameters.Add(Data9);
SqlParameter Data10 = new SqlParameter("@Data10", SqlDbType.Int);
Data10.Direction = ParameterDirection.Input;
Data10.Value = Data10in.Text;
insertParameters.Add(Data10);
SqlParameter Data11 = new SqlParameter("@Data11", SqlDbType.Int);
Data11.Direction = ParameterDirection.Input;
Data11.Value = Data11in.Text;
insertParameters.Add(Data11);
SqlParameter Data12 = new SqlParameter("@Data12", SqlDbType.Int);
Data12.Direction = ParameterDirection.Input;
Data12.Value = Data12in.Text;
insertParameters.Add(Data12);
SqlParameter Data13 = new SqlParameter("@Data13", SqlDbType.Int);
Data13.Direction = ParameterDirection.Input;
Data13.Value = Data13in.Text;
insertParameters.Add(Data13);
SqlParameter Data14 = new SqlParameter("@Data14", SqlDbType.Int);
Data14.Direction = ParameterDirection.Input;
Data14.Value = Data14in.Text;
insertParameters.Add(Data14);
SqlParameter Data15 = new SqlParameter("@Data15", SqlDbType.Int);
Data15.Direction = ParameterDirection.Input;
Data15.Value = Data15in.Text;
insertParameters.Add(Data15);
SqlParameter Data16 = new SqlParameter("@Data16", SqlDbType.Int);
Data16.Direction = ParameterDirection.Input;
Data16.Value = Data16in.Text;
insertParameters.Add(Data16);
SqlParameter Data17 = new SqlParameter("@Data17", SqlDbType.Int);
Data17.Direction = ParameterDirection.Input;
Data17.Value = Data17in.Text;
insertParameters.Add(Data17);
SqlParameter Data18 = new SqlParameter("@Data18", SqlDbType.Int);
Data18.Direction = ParameterDirection.Input;
Data18.Value = Data18in.Text;
insertParameters.Add(Data18);
SqlParameter Data19 = new SqlParameter("@Data19", SqlDbType.Int);
Data19.Direction = ParameterDirection.Input;
Data19.Value = Data19in.Text;
insertParameters.Add(Data19);
SqlParameter Data20 = new SqlParameter("@Data20", SqlDbType.Int);
Data20.Direction = ParameterDirection.Input;
Data20.Value = Data20in.Text;
insertParameters.Add(Data20);
SqlParameter Data21 = new SqlParameter("@Data21", SqlDbType.Int);
Data21.Direction = ParameterDirection.Input;
Data21.Value = Data21in.Text;
insertParameters.Add(Data21);
SqlDataSource2.Insert();
}
}
}
}
答案 0 :(得分:1)
请看这个示例,了解如何添加参数并将其作为模板。我将粘贴一些代码以帮助您入门,您将不得不填写其余的参数。
根据您计算WeekOf的方式,我会在using语句之外放置或获取该值,并将其传递给您尝试进行转换(Date,@ WeekOf,101)
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("InsertCommand="INSERT INTO [Table] WeekOf,User,OtherData)
VALUES (Convert(Date,@WeekOf,101), @User, @OtherData)"");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@WeekOf", WeekOfin.Text);
cmd.Parameters.AddWithValue("@User", YourUser.Text);
cmd.Parameters.AddWithValue("@OtherData", txtAddress.Text);
...
...
connection.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
//MessageDlg.Show(ex.Message); //do what ever kind of logging or error trapping here that you want..
}
//You will need to explain how you are populating or getting
// the MM/YY for WeekOf
//Personally I would refactor this code it's a bit unorthodox
}