C# 位置0处没有行

标签 c# sql .net

我再次收到错误,指出没有与 SQL 查询匹配的行。

它指出:

There is no row at position 0.

点在:

 firstnameTxt.Text = dt.Rows[0].ItemArray[1].ToString();

以下是我的C#代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["New"] != null)
        {
            redPnl.Visible = false;
            UserNameSess.Text += Session["New"].ToString();

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
            con.Open();

            SqlDataAdapter sda = new SqlDataAdapter("Select Student_Username, Student_FName, Student_SName, Student_Email FROM Student Where Student_Username=@StuUsername", con);
            sda.SelectCommand.Parameters.Add("@StuUsername", SqlDbType.VarChar).Value = UserNameSess.Text;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count > 0)

            usernameTxt.Text = dt.Rows[0].ItemArray[0].ToString();
            firstnameTxt.Text = dt.Rows[0].ItemArray[1].ToString();
            surnameTxt.Text = dt.Rows[0].ItemArray[2].ToString();
            emailTxt.Text = dt.Rows[0].ItemArray[3].ToString();
            dt.Clear();

        }
        else
        {
            redPnl.Visible = true;
        }



    }

我不知道它为什么要这样做,因为它诚实地带回了值(value)并将其显示在文本框中。

最佳答案

您忘记将大括号添加到您的 if 子句中:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["New"] != null)
    {
        redPnl.Visible = false;
        UserNameSess.Text += Session["New"].ToString();

        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
        con.Open();

        SqlDataAdapter sda = new SqlDataAdapter("Select Student_Username, Student_FName, Student_SName, Student_Email FROM Student Where Student_Username=@StuUsername", con);
        sda.SelectCommand.Parameters.Add("@StuUsername", SqlDbType.VarChar).Value = UserNameSess.Text;
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows.Count > 0)
        {  
           usernameTxt.Text = dt.Rows[0].ItemArray[0].ToString();
           firstnameTxt.Text = dt.Rows[0].ItemArray[1].ToString();
           surnameTxt.Text = dt.Rows[0].ItemArray[2].ToString();
           emailTxt.Text = dt.Rows[0].ItemArray[3].ToString();
           dt.Clear();
        }
    }
    else
    {
        redPnl.Visible = true;
    }
}

关于C# 位置0处没有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934964/

相关文章:

c# - 在 UnityContainer 中注册类型以使用其他命名注册来解析构造函数参数

c# - 如果使用 Razor MVC3 在 div 标记内声明

c# - 使用动态类型调用泛型扩展方法

SQL LIKE 特性-所有字符或所有数字

c# - 你如何让 NHibernate 忽略 POCO 中的属性

c# - 如何从 ISO 8601 格式创建 .NET DateTime

javascript - SignalR .net 核心。没有得到 signalr.js?

c# - 如何在 .NET 中找到 NumLock、CapsLock 和 ScrollLock 的状态?

mysql - SQL 查询 - 无法比较字段值?

mysql - 在 mysql 中查找最近的空闲时间段的查询 - 为什么它不起作用?