在ASP.NET和C#中,在system.data.dll中发生类型'system.indexoutofrangeexception'的异常但在用户代码中没有处理。在位置0处没有行。“ [英] In ASP.NET and C# "an exception of type 'system.indexoutofrangeexception' occurred in system.data.dll but was not handled in user code.there is no row at position 0."

查看:176
本文介绍了在ASP.NET和C#中,在system.data.dll中发生类型'system.indexoutofrangeexception'的异常但在用户代码中没有处理。在位置0处没有行。“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:

我从asp.net中的Web表单中获取数据库表中的数据和C#



我收到错误:



System.Data.dll中出现'System.IndexOutOfRangeException'类型的异常,但未处理用户代码



附加信息:位置0没有行。

i am fetching data from database table in a web form in asp.net and C#

Error I am receiving:

"An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll but was not handled in user code

Additional information: There is no row at position 0."





我尝试了什么:





What I have tried:

protected void btnsearch_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * from registrationinfo WHERE registrationNumber = @registrationNumber", con);
        cmd.Parameters.AddWithValue("@registrationNumber", txtsearch.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        {
            lblregistrationnumber.Text = dt.Rows[0]["registrationNumber"].ToString();
            txtadmissiondate.Text = dt.Rows[0]["admissionDate"].ToString();
            txtsession.Text = dt.Rows[0]["session"].ToString();
            txtstudentname.Text = dt.Rows[0]["studentName"].ToString();
            txtDOB.Text = dt.Rows[0]["studentDOB"].ToString();
            radiobtngender.SelectedValue = dt.Rows[0]["gender"].ToString();
            txtaddress.Text = dt.Rows[0]["address"].ToString();
            txtnationality.Text = dt.Rows[0]["nationality"].ToString();
            txtfathername.Text = dt.Rows[0]["fatherName"].ToString();
            txtmobile.Text = dt.Rows[0]["mobileNumber"].ToString();
            txtmailid.Text = dt.Rows[0]["email"].ToString();
            txtmothername.Text = dt.Rows[0]["motherName"].ToString();
            txtclass.Text = dt.Rows[0]["class"].ToString().Trim();
        }

推荐答案

机会是dt.Rows不包含任何行,因为查询没有返回任何行数据。



你应该在尝试访问它之前检查表是否有行(检查dt.Rows.Count> 0),这将阻止异常抛出并允许您在没有数据时优雅地处理。但是,如果您希望在此方案中使用数据,那么您需要检查数据库中的输入和数据,以尝试找出未返回任何内容的原因。
Chances are dt.Rows does not contain any rows as the query did not return any data.

You should always check the table has rows before you try to access them (check dt.Rows.Count > 0), that will stop an exception being thrown and allow you to gracefully handle when there is no data. However if you are expecting data in this scenario then you'll need to check your inputs and the data in your database to try and work out why nothing was returned.


这篇关于在ASP.NET和C#中,在system.data.dll中发生类型'system.indexoutofrangeexception'的异常但在用户代码中没有处理。在位置0处没有行。“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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