程序运行出现异常:在位置 0 处没有任何行
代码如下:运行出现错误:在位置 0 处没有任何行
private void btn_Sure_Click(object sender, System.EventArgs e)
{
try
{
DataRow dr;
SqlConnection myConnection;
myConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "conn_Default "]);
SqlDataAdapter da=new SqlDataAdapter( "select s.userid,s.name,s.ns_code,s.station,c.* from staff s left outer join cust c on s.ns_code=c.clno where s.userid=@userid and s.passwd=@passwd ",myConnection);
myConnection.Open();
da.SelectCommand.Parameters.Add(new SqlParameter( "@userid ", SqlDbType.VarChar,10));
da.SelectCommand.Parameters.Add(new SqlParameter( "@passwd ",SqlDbType.VarChar,10));
da.SelectCommand.Parameters[ "@userid "].Value = userid.Text.Trim();
da.SelectCommand.Parameters[ "@passwd "].Value=passwd.Text.Trim();
DataSet ds = new DataSet();
da.Fill(ds, "cust ");
dr = ds.Tables[0].Rows[0];
if(da.SelectCommand.ExecuteReader().Read())
{
Session[ "clno "]=dr[ "clno "].ToString();
Session[ "chname "]=dr[ "chname "].ToString();
Session[ "caddr "]=dr[ "caddr "].ToString();
Session[ "city "]=dr[ "city "].ToString();
Session[ "state "]=dr[ "state "].ToString();
Session[ "post_dist "]=dr[ "post_dist "].ToString();
Session[ "country "]=dr[ "country "].ToString();
Session[ "phone "]=dr[ "phone "].ToString();
Session[ "ctc_sale "]=dr[ "ctc_sale "].ToString();
Session[ "bill_cycle "]=dr[ "bill_cycle "].ToString();
Response.Redirect ( "Admin_Index.aspx ");
}
}
catch(Exception exp)
{
Response.Write ( "出现异常: " + exp.Message);
}
}
------解决方案--------------------dr = ds.Tables[0].Rows[0];
==> 这里没有查询到数据,
------解决方案--------------------dr = ds.Tables[0].Rows[0];
没有查询处数据,此句之前判断一下
if (ds.Tables[0].Rows.Count == 0)
return;
------解决方案--------------------if(ds.Tables[0].Rows.Count> 0)
{
dr = ds.Tables[0].Rows[0];
}
------解决方案--------------------if(da.SelectCommand.ExecuteReader().Read())
{
既然你用的是dr怎么又去执行查询命令,太影响效率了,建议你直接用
if(!((ds.tables.count <=0)||(ds.tables[0].rows.count <=0)))