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");


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

Edited 4 Years Ago by infogirl: bad code

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.

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?

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


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 article has been dead for over six months. Start a new discussion instead.