我的代码似乎存在问题:
C#
protected void ddlCalltype_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlCalltype.SelectedIndex != 0)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet dsFormat = new DataSet();
String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
using (var con = new SqlConnection(strConnString))
{
con.Open();
using (cmd = new SqlCommand("ROMA_UserManagement", con))
{
cmd.Parameters.Add("@flag", SqlDbType.VarChar).Value = "1";
cmd.Parameters.Add("@CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd.Parameters.Add("@Format", SqlDbType.VarChar).Value ="";
cmd.Parameters.Add("@disposition", SqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@SubDisposition", SqlDbType.VarChar).Value ="";
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
da.SelectCommand = cmd;
da.Fill(dsFormat);
}
con.Close();
}
ddlFormat.DataValueField = "DISPFORMAT";
ddlFormat.DataTextField = "FORMATDETAIL";
ddlFormat.DataSource = dsFormat.Tables[0];
ddlFormat.DataBind();
ddlFormat.Items.Insert(0, "<----Select---->");
using (var con = new SqlConnection(strConnString))
{
con.Open();
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SqlCommand cmd1 = new SqlCommand();
SqlDataAdapter da1 = new SqlDataAdapter();
using (cmd1 = new SqlCommand("ROMA_UserManagement", con))
{
cmd1.Parameters.Add("@flag", SqlDbType.VarChar).Value = "0";
cmd1.Parameters.Add("@CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd1.Parameters.Add("@Format", SqlDbType.VarChar).Value = ddlFormat.SelectedValue.ToString();
cmd1.Parameters.Add("@disposition", SqlDbType.VarChar).Value = ddlDisp.SelectedValue.ToString();
cmd1.Parameters.Add("@SubDisposition", SqlDbType.VarChar).Value = ddlSubdisp.SelectedValue.ToString();
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.ExecuteNonQuery();
da.SelectCommand = cmd1;
da1.Fill(ds1);
dt = ds1.Tables[0];
gvDetails.DataSource = ds1;
gvDetails.DataBind();
}
con.Close();
}
}
else
{
ddlFormat.Items.Clear();
}
}
大家好。我知道这个问题一再被问过......但是我无法从答案中得到答案,请告诉我任何事情......我做错了什么。
答案 0 :(得分:0)
嗨尝试在命令对象
之后初始化Sqldata适配器这只是一个例子
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da=null;
DataSet dsFormat = new DataSet();
String strConnString =
ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
using (var con = new SqlConnection(strConnString))
{
con.Open();
using (cmd = new SqlCommand("ROMA_UserManagement", con))
{
cmd.Parameters.Add("@flag", SqlDbType.VarChar).Value = "1";
cmd.Parameters.Add("@CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd.Parameters.Add("@Format", SqlDbType.VarChar).Value ="";
cmd.Parameters.Add("@disposition", SqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@SubDisposition", SqlDbType.VarChar).Value ="";
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
da=new SqlDataAdapter(cmd);
da.Fill(dsFormat);
}
con.Close();
}
ddlFormat.DataValueField = "DISPFORMAT";
ddlFormat.DataTextField = "FORMATDETAIL";
ddlFormat.DataSource = dsFormat.Tables[0];
ddlFormat.DataBind();
ddlFormat.Items.Insert(0, "<----Select---->");
答案 1 :(得分:0)
首先,您已将select命令分配给此部分中的错误SqlDataAdapter
实例:
da.SelectCommand = cmd1;
da1.Fill(ds1);
这会使da1
实例保持打开状态而不应用任何选择命令,因此抛出异常。正确的分配应该是da1.SelectCommand = cmd1;
。
执行查询时抛出的第二个问题Cannot find table 0
包含空结果,因此dsFormat.Tables[0]
或ds1.Tables[0]
变为空,无法分配给数据绑定。
要检查查询是否产生结果,请使用if-condition检查表计数,如下例所示:
if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
{
// grid data binding here
}
通过组合这些解决方案,数据库连接应按如下所示组成:
using (var con = new SqlConnection(strConnString))
{
con.Open();
using (cmd = new SqlCommand("ROMA_UserManagement", con))
{
cmd.Parameters.Add("@flag", SqlDbType.VarChar).Value = "1";
cmd.Parameters.Add("@CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd.Parameters.Add("@Format", SqlDbType.VarChar).Value ="";
cmd.Parameters.Add("@disposition", SqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@SubDisposition", SqlDbType.VarChar).Value ="";
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
da.SelectCommand = cmd;
da.Fill(dsFormat);
}
con.Close();
}
if (dsFormat.Tables.Count > 0 && dsFormat.Tables[0].Rows.Count > 0)
{
ddlFormat.DataValueField = "DISPFORMAT";
ddlFormat.DataTextField = "FORMATDETAIL";
ddlFormat.DataSource = dsFormat.Tables[0];
ddlFormat.DataBind();
ddlFormat.Items.Insert(0, "<----Select---->");
}
using (var con = new SqlConnection(strConnString))
{
con.Open();
using (cmd1 = new SqlCommand("ROMA_UserManagement", con))
{
cmd1.Parameters.Add("@flag", SqlDbType.VarChar).Value = "0";
cmd1.Parameters.Add("@CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd1.Parameters.Add("@Format", SqlDbType.VarChar).Value = ddlFormat.SelectedValue.ToString();
cmd1.Parameters.Add("@disposition", SqlDbType.VarChar).Value = ddlDisp.SelectedValue.ToString();
cmd1.Parameters.Add("@SubDisposition", SqlDbType.VarChar).Value = ddlSubdisp.SelectedValue.ToString();
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.ExecuteNonQuery();
da1.SelectCommand = cmd1;
da1.Fill(ds1);
if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
{
dt = ds1.Tables[0];
gvDetails.DataSource = ds1; // are you mean to assign dt here?
gvDetails.DataBind();
}
}
con.Close();
}
通过执行检查以在将数据绑定应用于控件之前检查DataSet
/ DataTable
中的空结果,之后可以避免异常。
修改强>
根据OP的评论,给定的存储过程需要输出参数,可以这样实现:
// change SqlDbType.Varchar to other data type required for output parameter (Int, DateTime, etc.)
var output = new SqlParameter("@outputparamname", SqlDbType.Varchar) { Direction = ParameterDirection.Output };
// change or also apply to `cmd` depending which `SqlCommand` requires output parameter
cmd1.Parameters.Add(output);