SQL Server C# windows form 'There is no row at position 0.' [closed]

xriantvc  于 2022-12-22  发布在  C#
关注(0)|答案(1)|浏览(440)

Closed. This question needs debugging details . It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem . This will help others answer the question.
Closed 3 days ago.
Improve this question
I'm using C# windows form .NET
The main idea I'm trying to make is that after the customer logging in, he opens a 'Profile' Form where I want to display his username & password as labels in this form.

private void Form4_Load(object sender, EventArgs e)
        {           
            sqlcon.Open();
            com = new SqlCommand(str, sqlcon);

            string query = "Select Username,Password from Account where Username = '" + thename + "'";

            using (SqlCommand command = new SqlCommand(query, sqlcon))
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);

                label4.Text = (string)dt.Rows[0]["Username"];
                label5.Text = (string)dt.Rows[0]["Password"];

            }
        }

I'm getting an error:
System.IndexOutOfRangeException: 'There is no row at position 0.'
"the name" in the query is the string passed from another Form referring to the username that logged in. I've tried this method and this same code in another form for other purposes and it worked fine.

SqlCommand cmd;
            cmd = new SqlCommand("Select Username,Password from Account where Username = '" + thename + "'", sqlcon);

            foreach (DataRow dr in dt.Rows)
            {

               label4.Text = dr["Username"].ToString(); ;
               label5.Text = dr["Password"].ToString(); ;

            }
sqlcon.Open();
            com = new SqlCommand(str, sqlcon);

            string query = "Select Username,Password from Account where Username = '" + thename + "'";

            using (SqlCommand command = new SqlCommand(query, sqlcon))
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    label4.Text = (string)dt.Rows[0]["Username"];
                    label5.Text = (string)dt.Rows[0]["Password"];
                }

            }

I've tried this code. It gives no errors but it doesn't do anything when I run the code.
I need ideas.
Thank you.

bvjveswy

bvjveswy1#

Maybe when the form load you do not have thename value set yet.
If thename is a class-level variable in the Form4 class, you can set it as static and assign it before you call Form4 to show.
Another way to encapsulate the user information is to create a new static class with a static property to hold the user logged in. So you can assign it when it logs in and can retrieve wherever you want to in your forms.

public static class SessionInfoService
{
    public static string TheName { get; set; }
}

One last point another contributor also pointed. Use SqlParameter object instead of concatenating the value within the SQL command. It is strongly recommended to avoid SQL injection.

相关问题