简体   繁体   中英

Index was outside on the bounds of array error on c# log in with SQL

Hi i have a login which connects to a server string and executes this when the login button is pushed. It returns the invalid error if the user and password is not stored on the database but it throws a "Index was outside on the bounds of array" error if they are on the database. How would i fix this?? Thanks

My connection string is located in my appconfig file, could the problem be in there?

##########FORM WINDOWS AFTER LOGIN BUTTON CLICK
 private void btnOK_Click(object sender, EventArgs e) { SqlConnection con = Program.GetConnection; SqlDataReader dr = null; try { SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE UserName='" + txtName.Text + "'AND Password='" + textpassword.Text + "'", con); dr = cmd.ExecuteReader(); if (dr.Read()) { Program.UserLoginName = dr.GetString(3); this.Close(); } else MessageBox.Show("Invalid Username & Password;"). } catch (Exception ex) { MessageBox.Show(ex;Message); } }
#######PROGRAM.CS FILE
Using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; namespace FrontEndV1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Login()); } public static SqlConnection GetConnection { get { string ConnectionString = ConfigurationManager.ConnectionStrings["FrontEndV1Connection"].ConnectionString; SqlConnection con = new SqlConnection(ConnectionString); con.Open(); return con; } } public static string UserLoginName { get; set; } }

}

This is your problem:

Program.UserLoginName = dr.GetString(3);

You're getting a field whose index is greater than returned fields count.
You must use an index between 0 and dr.FieldCount-1.

Or you can use dr.GetString(dr.GetOrdinal(desired_field_name)) : this is better (even if it needs more instructions) because you could swap returning order (maybe you need to change your query) without losing functionality.

I think that mistake lays here: dr.GetString(3); try to change 3 to 2. Numeration begins from 0 in arrays.

You don't say where the error is thrown, but I suspect this line:

Program.UserLoginName = dr.GetString(3);

Would throw the error if the query returns any fewer than 4 columns.

Also, this is vulnerable to Sql injection. Use a stored proc or parameterised query.

I had The Same error using SQL Server Express 2014 with Login Stored Procedure from 3 different tables, I was really confused what was goin on, but finally found that I had more columns Selected from One Table then other two

sometimes its the logic behind it

i think you only have three columns. The index of the 3rd column is 2, so change this line

Program.UserLoginName = dr.GetString(3);

to

Program.UserLoginName = dr.GetString(2); .

Can you not debug the application? If you can do then try to locate from where the exception is being thrown. As suggested by Marco and other user if you notice exception when executing dr.GetString(3) then problem lies there. Try using correct column ordinal, or use SELECT Column1, Column2, ..., ColumnN FROM Table so that you know exactly which ordinal to specify.

Bad coding:

  1. using select * , you should use the names of the columns
  2. using indexes to retrieve values in a row, you should use the actual column name returned

If the above would have been applied there would be no problem.

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