例外:位置0没有行。 [英] Exception:There is no row at position 0.

查看:174
本文介绍了例外:位置0没有行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:Plz Provide Me Solution

  protected   void  btnsubmit_Click( object  sender,EventArgs e)
{
尝试
{
// 检索用户ID。
cmd = new SqlCommand( 从中选择nuser_id tbl_User_Info其中suser_login =' + ListBox1.SelectedValue + ',con);
DataSet ds10 = new DataSet();
DataTable dt10 = new DataTable();
SqlDataAdapter da10 = new SqlDataAdapter(cmd);
da10.Fill(ds10);
string uId = ds10.Tables [ 0 ]。行[ 0 ] [ nuser_id]。ToString();
int userId = Convert.ToInt32(uId);
}
catch (例外情况)
{
}
}

解决方案

使用参数和ExecuteScalar

 cmd =  new  SqlCommand( 从tbl_User_Info中选择nuser_id,其中suser_login = @ Para1,con); 
cmd.Parameters.AddWithValue( @ Para1,ListBox1.SelectedValue);
int ? id =( int ?)cmd.ExecuteScalar();
if (id.HasValue)
{
// < span class =code-comment>有ID值,使用id.Value获取值
}


试试这个...



  protected   void  btnsubmit_Click( object  sender,EventArgs e)
{
尝试
{
// 检索用户ID。
cmd = new SqlCommand( 从tbl_User_Info中选择nuser_id其中suser_login =' + ListBox1.SelectedValue + ',con);
DataSet ds10 = new DataSet();
DataTable dt10 = new DataTable();
SqlDataAdapter da10 = new SqlDataAdapter(cmd);
da10.Fill(ds10);
if (ds10.Tables [ 0 ]。Rows.Count> 0)
{
string uId = ds10.Tables [ 0 ]。行[ 0 ] [ nuser_id]。ToString() ;
int userId = Convert.ToInt32(uId);
}
}
catch (例外情况)
{
}
}


This Is My code:"Plz Provide Me Solution"

protected void btnsubmit_Click(object sender, EventArgs e)
{
 try
   {
   //Retrieving user id.
cmd = new SqlCommand("select nuser_id from tbl_User_Info where suser_login = '"+ListBox1.SelectedValue+"'", con);
   DataSet ds10 = new DataSet();
   DataTable dt10 = new DataTable();
   SqlDataAdapter da10 = new SqlDataAdapter(cmd);
   da10.Fill(ds10);
   string uId = ds10.Tables[0].Rows[0]["nuser_id"].ToString();
   int userId = Convert.ToInt32(uId);
   }
  catch (Exception ex)
  {
  }       
}

解决方案

use parameter and ExecuteScalar

cmd = new SqlCommand("select nuser_id from tbl_User_Info where suser_login =@Para1", con);
cmd.Parameters.AddWithValue("@Para1", ListBox1.SelectedValue);
int? id = (int?)cmd.ExecuteScalar();
if(id.HasValue)
{
  // there is value for ID, use id.Value to get the value 
}


Try this...

protected void btnsubmit_Click(object sender, EventArgs e)
{
 try
   {
   //Retrieving user id.
cmd = new SqlCommand("select nuser_id from tbl_User_Info where suser_login = '"+ListBox1.SelectedValue+"'", con);
   DataSet ds10 = new DataSet();
   DataTable dt10 = new DataTable();
   SqlDataAdapter da10 = new SqlDataAdapter(cmd);
   da10.Fill(ds10);
   if(ds10.Tables[0].Rows.Count >0)
   {
   string uId = ds10.Tables[0].Rows[0]["nuser_id"].ToString();
   int userId = Convert.ToInt32(uId);
   }
   }
  catch (Exception ex)
  {
  }       
}


这篇关于例外:位置0没有行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆