0

I need to check & capture user login details, and store it under Session to carry forward to next page. But i am getting this error, DataTable does not have definition for "Getvalue" at Session["idname"]= dt.GetValue(0).ToString();. The code i used,

con.Open();
SqlCommand cmd = new SqlCommand("select * from LoginDB where (EmpCode COLLATE Latin1_General_CS_AS = @EmpCode) and (Password COLLATE Latin1_General_CS_AS =@Password)", con);
cmd.Parameters.AddWithValue("@EmpCode", txtLogin.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPwd.Text.Trim());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
    Session["idname"]= dt.GetValue(0).ToString();
    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('" + "Login Success!" + "')</script>");
    Session["identity"] = txtLogin.Text;
    Response.Redirect("Mainpage.aspx", false);
}
else
{
    txtLogin.Text = "";
    ShowMessage("UserId / Password is Not Correct!");
}
con.Close();
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
santosh
  • 71
  • 8

3 Answers3

1

The error message is clear: DataTable does not have a definition for "Getvalue".

Since DataTable has more than just one row, what you can do is to select the first row and get the value via specified column of that row:

if (dt.Rows.Count > 0)
{
    var userRow = dt.Rows[0];
    Session["idname"] = userRow["idName"].ToString(); //assuming you have idName column in LoginDB
    //..
}
else
{
    txtLogin.Text = "";
    ShowMessage("UserId / Password is Not Correct!");
}
Console.WriteLine("Hello World!");

See: Get Cell Value from a DataTable in C#

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
0

Change this:

Session["idname"]= dt.GetValue(0).ToString();

To:

Session["idname"] = dt.Rows[0][0].ToString();
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

There is no built in function that gives you a value that is place in the datatable. Try to use object of datarow to fetch data with in row. Below is the code. Try this one.

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from LoginDB where (EmpCode COLLATE Latin1_General_CS_AS = @EmpCode) and (Password COLLATE Latin1_General_CS_AS =@Password)", con);
    cmd.Parameters.AddWithValue("@EmpCode", txtLogin.Text.Trim());
    cmd.Parameters.AddWithValue("@Password", txtPwd.Text.Trim());
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    
    if (dt.Rows.Count > 0)
    {
        DataRow dr=dt.Rows[0];
        Session["idname"]= dr[0].ToString();
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('" + "Login Success!" + "')</script>");
        Session["identity"] = txtLogin.Text;
        Response.Redirect("Mainpage.aspx", false);
    }
    else
    {
        txtLogin.Text = "";
        ShowMessage("UserId / Password is Not Correct!");
    }
    con.Close();
Abdul Haseeb
  • 514
  • 4
  • 13