我有50种不同格式的中继器。每个人都必须根据国家进行约束。
我创建了一个带复选框的gridview。检查哪一行以在相应的转发器中显示那些行数据。
我向存储过程发送多个参数并返回一个数据表。我遍历数据表并点击按钮我得到所有中继器,除非状态相同 - 它只返回最后一个。我调试并且代码遍历它但覆盖了具有该状态的前一行。如何显示同一状态的所有中继器?
protected void GetVinData()
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DBConnection"]);
SqlCommand cmmd = new SqlCommand();
cmmd.CommandType = CommandType.StoredProcedure;
cmmd.CommandText = c
cmmd.Connection = cn;
cn.Open();
try
{
cmmd.Parameters.Add("@POLICY", SqlDbType.VarChar);
cmmd.Parameters["@POLICY"].Value = ddlPolicy.SelectedValue;
cmmd.Parameters.Add("@VIN", SqlDbType.VarChar);
cmmd.Parameters["@VIN"].Value = txtMsg.Value;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmmd);
adapter.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DBConnection"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllVinNumbers";
cmd.Connection = conn;
cmd.Parameters.Add("@POLICY", SqlDbType.VarChar);
cmd.Parameters["@POLICY"].Value = ddlPolicy.SelectedValue;
cmd.Parameters.Add("@VIN", SqlDbType.VarChar);
cmd.Parameters["@VIN"].Value = dr["VIN"].ToString();
if (dr["STATE"].ToString() == "AL")
{
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
Repeater_AL.DataSource = cmd.ExecuteReader();
Repeater_AL.DataBind();
Repeater_AL.Visible = true;
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
lblMessage.Visible = true;
}
finally
{
conn.Close();
conn.Dispose();
}
}
else if (dr["STATE"].ToString() == "AK")
{
try
{
if (conn.State != ConnectionState.Open)
conn.Open();
Repeater_AK.DataSource = cmd.ExecuteReader();
Repeater_AK.DataBind();
Repeater_AK.Visible = true;
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
lblMessage.Visible = true;
}
finally
{
conn.Close();
conn.Dispose();
}
}
else if (dr["STATE"].ToString() == "AZ")
{
try
{
if (conn.State != ConnectionState.Open)
conn.Open();
Repeater_AZ.DataSource = cmd.ExecuteReader();
Repeater_AZ.DataBind();
Repeater_AZ.Visible = true;
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
lblMessage.Visible = true;
}
finally
{
conn.Close();
conn.Dispose();
if (Repeater_AZ.Visible == true)
{
Repeater_AZ.Visible = true;
}
}
} ... and so on for 50 states
答案 0 :(得分:0)
这种轻微的重构可能会有所帮助:
protected void BindVin(string state)
{
Repeater rpt = null;
Control ctl = null;
string name = string.Empty;
SqlConnection conn = null;
SqlCommand cmd = null;
name = "Repeater_" + state;
ctl = Page.FindControl(name);
rpt = ctl as Repeater;
if (rpt != null) {
try {
conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings("DBConnection"));
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllVinNumbers"; // should this have the state to get VINs for?
cmd.Connection = conn;
cmd.Parameters.Add("@POLICY", SqlDbType.VarChar);
cmd.Parameters("@POLICY").Value = ddlPolicy.SelectedValue;
cmd.Parameters.Add("@VIN", SqlDbType.VarChar);
cmd.Parameters("@VIN").Value = dr("VIN").ToString();
if (conn.State != ConnectionState.Open) {
conn.Open();
}
rpt.DataSource = cmd.ExecuteReader();
rpt.DataBind();
rpt.Visible = true;
conn.Close();
conn.Dispose();
} catch (Exception ex) {
lblMessage.Text = ex.Message;
lblMessage.Visible = true;
} finally {
conn.Close();
conn.Dispose();
}
}
}
protected void GetVinData()
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DBConnection"]);
SqlCommand cmmd = new SqlCommand();
cmmd.CommandType = CommandType.StoredProcedure;
cmmd.CommandText = c
cmmd.Connection = cn;
cn.Open();
try
{
cmmd.Parameters.Add("@POLICY", SqlDbType.VarChar);
cmmd.Parameters["@POLICY"].Value = ddlPolicy.SelectedValue;
cmmd.Parameters.Add("@VIN", SqlDbType.VarChar);
cmmd.Parameters["@VIN"].Value = txtMsg.Value;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmmd);
adapter.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
BindVin(dr["STATE"].ToString());
}
}
}