0

I am trying to get the usert_id value from datatable and assign it to session variable but I am not able to get the user_id in my code. How can I do this?

try
        {
            conn.Open();
            MySqlCommand cmd = new MySqlCommand("Select * from students where user_name='" + loginname.Text + "' and user_password ='" + password.Text + "'", conn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                ShowMessage(dt.Row['.user_id.']); //<-- Problem happens here
                Session["user_id"] = "bar";
                Response.Redirect("dashboard.aspx");
            }
            else
            {
                Response.Write("<script>alert('Please enter valid Username and Password')</script>");
            }
        }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
CJAY
  • 6,989
  • 18
  • 64
  • 106
  • Hi do you get kind of error or something ? – Max Nov 01 '15 at 09:51
  • http://stackoverflow.com/questions/13816490/get-cell-value-from-a-datatable-in-c-sharp – Anik Islam Abhi Nov 01 '15 at 09:52
  • Should be using double quotes (") not an apostrophe (') –  Nov 01 '15 at 10:40
  • i think problem in ".user_id" to get the result , for more info you can follow this link . http://stackoverflow.com/questions/1774498/how-to-iterate-through-a-datatable Hope it will help for you – Pankaj Gupta Dec 20 '15 at 05:49
  • data table is like a table . it contains datarows. if you select returns just one row , you shoul read the first datarow of DT then your selected field. just like jeremy answered – Ali pishkari Dec 20 '15 at 06:06

1 Answers1

3

.user_id. is not a Row index and I doubt it's the column name... you didn't surround it with dots did you? I think it should be using Rows not Row and double quotes:

ShowMessage(dt.Rows[0]["user_id"].ToString());
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321