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;

Recommended Answers

All 3 Replies

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:

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.