Jaquarh
Jaquarh

Reputation: 6693

C# : How to check a DataRow against a String

I current have a connection open which just queries through with a dependency of user input. My Objects are correct and I have set-up the Instances related to the Objects and it is all working fine.

I now need to match if the user input is the same as the stored Database data however, it does not seem to be working; i am getting no errors though.

After doing a bit of my own debugging and researching, I tried to convert my DataRow to a String using ToString(); but still no luck.

Could anyone possible take a look into this section of code and possibly help me try to figure this out? Many thanks.

DataSet ds = new DataSet();
query = new MySqlDataAdapter(SQL, conn);
query.Fill(ds, "AllData");
DataTable dt = new DataTable();
dt = ds.Tables["AllData"];
DataRow[] r = dt.Select();
int i = 0;
while (i != r.Length)
    {
     string toTest = r[i]["BusinessID"].ToString();
     if(toTest == sinput)
    {

Note: sinput is the Users input and r[i]["BusinessID"] contains the data in the database which I am trying to match against the input string.

Edit: I have not passed anything into the dt.Select() for security purposes as I do not want direct user input into a query.

Upvotes: 2

Views: 452

Answers (1)

VCody
VCody

Reputation: 516

Please find the code:Instead of data from database i have hard code the value. Hope,it will help you.

        string Name = "Test1";
        int Id = 1;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Id",typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Rows.Add(1,"Test");
        dt.Rows.Add(1, "Test1");
        DataRow[] r = dt.Select();
        int i = 0; //Not Required.
        while (r.Length>0)
        {
            string toTest = r[i]["Name"].ToString();
            int toTest1 =Convert.ToInt32(r[i]["Id"]);
            if (toTest == Name)
            {
                Console.WriteLine(toTest);
            }
            if (toTest1 == Id)
            {
                Console.WriteLine(toTest1);
            }
        }

Upvotes: 3

Related Questions