简体   繁体   中英

C# There is no row at position 0

Im getting the error once again stating there is no row that matches the SQL query.

It states:

There is no row at position 0.

The points at:

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

The following is my C# code.

    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;
        }



    }

I have no idea why it is doing it as its honestly bringing back the value and showing it in the textbox.

You forgot to add the curly braces to your if clause:

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;
    }
}

You need to enclose the statements under the if with a pair of {} otherwise the condition will be applied to the immediate next statement only; others will execute as usual

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();
}

You can omit the {} if there is only one statement needed to execute based on the condition, in all other cases you should enclose the statements within the pair of {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM