Ceejay
Ceejay

Reputation: 7297

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

Upvotes: 0

Views: 365

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65712

.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());

Upvotes: 3

Related Questions