我有这段代码可以通过文本框从用户那里接收一个数字,然后使用该值和SQL表中的两个值进行简单的计算。我进行了调试,未收到任何错误,但gridview中的标签没有填充。任何帮助将不胜感激。
protected void Calculate_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT [Divide], [Calc] FROM [Parrts] WHERE (([Region] = 'East Coast') AND ([Model] = @Model) AND ([SashSize] = @SashSize) AND ([Operation] = @Operation) AND ([PartDesc] = @PartDesc) AND ([Description] = @Description) AND ([Lites] =@Lites))", conn);
SqlParameter Model = new SqlParameter("@Model", ddModel.SelectedValue);
cmd.Parameters.Add(Model);
SqlParameter SashSize = new SqlParameter("@SashSize", ddSashSize.SelectedValue);
cmd.Parameters.Add(SashSize);
SqlParameter Operation = new SqlParameter("@Operation", ddOperation.SelectedValue);
cmd.Parameters.Add(Operation);
SqlParameter Part = new SqlParameter("@PartDesc", ddPart.SelectedValue);
cmd.Parameters.Add(Part);
SqlParameter Color = new SqlParameter("@Description",ddColor.SelectedValue);
cmd.Parameters.Add(Color);
SqlParameter Lites = new SqlParameter("@Lites",ddLites.SelectedValue);
cmd.Parameters.Add(Lites);
conn.Open();
cmd.Connection = conn;
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Int32 Divide = (int)rdr["Divide"];
double Calc = (double)rdr["Calc"];
int HW = Convert.ToInt32(length_txt.Text);
foreach (GridViewRow row in GridViewAMI.Rows)
{
string Length = ((Label)row.FindControl("Length")).Text;
Response.Write(Length);
Length = ((HW / Divide) - Calc).ToString();
}
}
rdr.Close();
cmd.Dispose();
conn.Close();
}
答案 0 :(得分:0)
由于我看不到您再次填充标签,因此可能会有所帮助:
foreach (GridViewRow row in GridViewAMI.Rows)
{
// Get the Label safely so you can manipulate it later
Label rowLabel = row.FindControl("Length") as Label;
// If you - for whatever reason - can't get the Label, skip this row
if(rowlabel == null)
continue;
string length = rowLabel.Text;
Response.Write(length);
length = ((HW / Divide) - Calc).ToString();
// Populate the Label with the new Data
rowLabel.Text = length;
}
答案 1 :(得分:0)
尝试一下:
foreach (GridViewRow row in GridViewAMI.Rows)
{
Label lbl = (Label)row.FindControl("Length");
string length = lbl.Text;
Response.Write(length);
length = ((HW / Divide) - Calc).ToString();
lbl.Text = length;
}
您忘了将文本放回Label
。