0

Hello,
i have encountered with a problem in one of methods comparing string from database to the string named pword that i initialized from textbox. Can anyone know the easiest for begginer to fix it? I would be very very greatful.

                SqlConnection myConnection = new SqlConnection();
                myConnection.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Studijos\\c#\\KursinisDarbas\\BazineAplikacija\\BazineAplikacija\\DuomenuBaze.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

                DataSet myDataSet = new DataSet();

                string sql = "SELECT *From tblWorkers";

                SqlDataAdapter myAdapter = new SqlDataAdapter(sql, myConnection);
                myAdapter.Fill(myDataSet, "Workers");

                myConnection.Open();

                int MaxRows = myDataSet.Tables["Workers"].Rows.Count;
                bool permition = false;
                for (int i = 0; i < MaxRows; i++)
                {
                    DataRow myRow = myDataSet.Tables["Workers"].Rows[i];
                    MessageBox.Show(myRow.ItemArray.GetValue(4).ToString());
                    if (Convert.ToInt32(myRow.ItemArray.GetValue(0).ToString()) == ID)
                    {
                        if (pword == myRow.ItemArray.GetValue(4).ToString())
                        {
                            permition = true;
                        }
                        else
                        {
                            MessageBox.Show("Wrong password");
                        }
                    }
                }
                myConnection.Close();
                return permition;

Edited by infogirl: bad code

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by PatSharbaugh
0

Well, the first problem I think is that you are trying to fill your dataset from the adapter before you open a connection to the database. Try flipping lines 9 and 11.

0

Thank you for replying, I have changed that but still comparing pword and myRow.ItemArray.GetValue(4).ToString() i get false, although pword's and variable from database (it's actual type is nvarchar50). Can it be the problems of types?

0

One thing you could do to help debug is to add this line before the if statement:

MessageBox.Show(myRow.ItemArray.GetValue(4).ToString());

Also, when comparing strings, you can use the String.Compare() method with a compare option to StringComparison.InvariantCultureIgnoreCase.

if (string.Compare(pword, myRow.ItemArray.GetValue(4).ToString(), StringComparison.InvariantCultureIgnoreCase))

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.