Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I am trying to fetch data from a table in SQL Server to Label in my Windows Form. It is showing me a null exception error and I am unable to figure out why.

I am getting this error:
System.IndexOutOfRangeException: '@Name' on the following line:
NameLabel.Text = reader["@Name"].ToString();

I am storing it in a string because I am calling the value in another form.

Please help.

What I have tried:

string queryText = @"SELECT Count(*) FROM NewsClip_Login 
                             WHERE Username = @Username AND Password = @Password";

                using (SqlConnection cn = new SqlConnection("Data Source=PRIMO-CHALICE;Initial Catalog=NewsClip;Integrated Security=True"))
                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text);
                    cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text);
                    cmd.Parameters.AddWithValue("@Name", NameLabel.Text);
                    SqlDataReader reader = cmd.ExecuteReader();

                    reader.Read();
                    NameLabel.Text = reader["@Name"].ToString();
                    NameLabelText = NameLabel.Text;
                    reader.Close();

                    int result = (int)cmd.ExecuteScalar();
                    if (result > 0)
                    {
                        this.Hide();
                        NewsClip_Main nc_main = new NewsClip_Main();
                        nc_main.Enabled = true;
                        nc_main.ShowDialog();

                        this.Close();
                    }
                    else
                        MessageBox.Show("User Not Found!");
                    cn.Close();
Posted
Updated 10-May-18 1:22am

You don't have a parameter called @name in your SQL statement. Perhaps you wanted to select it like
SELECT Name FROM NewsClip_Login WHERE Username = @Username AND Password = @Password";

If that is what you're trying to do then you refer to the result columns wiuthout @ since that is used for parameters.

Another thing concerning the password. Never ever store the passwords as plain text. They should always be hashed one-way. Have a look at Password Storage: How to do it.[^]

ADDITION
-----------
string queryText = @"SELECT Name FROM NewsClip_Login WHERE Username = @Username AND Password = @Password";

using (SqlConnection cn = new SqlConnection("Data Source=PRIMO-CHALICE;Initial Catalog=NewsClip;Integrated Security=True")) {
   using (SqlCommand cmd = new SqlCommand(queryText, cn))
   {
      cn.Open();
      cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text);
      cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text);
      SqlDataReader reader = cmd.ExecuteReader();

      reader.Read();
      NameLabel.Text = reader["Name"].ToString();
      NameLabelText = NameLabel.Text;
      reader.Close();
      ...
 
Share this answer
 
v3
Comments
Primo Chalice 10-May-18 7:36am    
The error is still there.
Wendelius 10-May-18 7:40am    
Did you also remove the code to add the parameter to your command. Have a look at the modified answer.
Primo Chalice 11-May-18 0:37am    
I tried the modified code and I guess it worked but came up with a new error.
"System.InvalidCastException: 'Specified cast is not valid.'" on "int result = (int)cmd.ExecuteScalar();"
Wendelius 11-May-18 0:45am    
ExecuteScalar returns data from the first column of the first row in the result set. Now that we have changed the COUNT(*) to Name it doesn't return an int anymore.

If you still need the count, you need to make a separate query for that. However, id the count really needed, can there be multiple users with same name and password? After all, when you fetch with the reader you already know if the user exists or not.
Primo Chalice 11-May-18 0:57am    
So now in the (if condition), what shall I write to check whether the user exists or not?
Sorry I didn't realize that your SQL statement not having Name column

SELECT Count(*) FROM NewsClip_Login

You should have columnname if you are using reader for example if you are fetching the value of name then you should have name column in your select statement.

Remove @ from the reader it should be reader ["Name"]
 
Share this answer
 
v2
Comments
Primo Chalice 10-May-18 7:33am    
The error is still there.
System.IndexOutOfRangeException: 'Name'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900