简体   繁体   中英

How to get cell or row value from DataTable in c#

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

.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 Row s not Row and double quotes:

ShowMessage(dt.Rows[0]["user_id"].ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM