Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=1GCAttendanceManagementSystem;Integrated Security=True");
DataTable dt = new DataTable();
con.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select * from Employee where EmpUsername='" + Session["id"] + "'", con);

myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
txtCode.Text = (myReader["EmployeeId"].ToString());
txtUsername.Text = (myReader["EmpUsername"].ToString());
txtPass.Text = (myReader["EmpPassword"].ToString());
txtEmail.Text = (myReader["EmpEmail"].ToString());
txtFirstname.Text = (myReader["EmpFirstName"].ToString());
txtLastname.Text = (myReader["EmpLastName"].ToString());
txtGender.Text = (myReader["EmpGender"].ToString());
txtContact.Text = (myReader["EmpContact"].ToString());
txtAddress.Text = (myReader["EmpAddress"].ToString());
txtDept.Text = (myReader["EmpDept"].ToString());

}
con.Close();

What I have tried:

I have tried the coding. But all the textbox are blank. Nothing happen at all.
Posted
Updated 12-Feb-18 8:39am
Comments
F-ES Sitecore 11-Feb-18 10:50am    
If the code doesn't go into the while loop (use the debugger to step through) then your query is returning no rows. That will be because the session["id"] isn't want you expect or there is nothing in the database that matches it. We don't know what is in your session or your database so can't say much more than that.
Richard Deeming 13-Feb-18 11:05am    
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

using (SqlConnection con = new SqlConnection("..."))
using (SqlCommand myCommand = new SqlCommand("select * from Employee where EmpUsername = @id", con))
{
    myCommand.Parameters.AddWithValue("@id", Session["id"]);
    
    con.Open();
    using (SqlDataReader myReader = myCommand.ExecuteReader())
    {
        if (myReader.Read())
        {
            txtCode.Text = Convert.ToString(myReader["EmployeeId"]);
            ...
        }
    }
}
f farihin 21-Feb-18 7:15am    
It works now

0) Put your code in a try/catch block.

1) At the top of the try block, put this like

C#
if (string.IsNullOrEmpty(Session["id"])) { throw new Exception("Session ID has not been set");}


2) Run the code under the debugger with a breakpoint set in the catch block.

3) Your code is generally not really built well. Do it this way instead:

C#
try
{
    if (string.IsNullOrEmpty(Session["id"])) 
    {
        // throw an exception so that the code doesn't perform an unnecessary 
        // database query
        throw new Exception("Session ID has not been set");
    }
    using (SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=1GCAttendanceManagementSystem;Integrated Security=True"))
    {
        DataTable dt = new DataTable();
        con.Open();
        SqlDataReader myReader = null;
        using (SqlCommand myCommand = new SqlCommand("select * from Employee where EmpUsername='" + Session["id"] + "'", con){CommandType=CommandType.Text})
        {
            myReader = myCommand.ExecuteReader();
            if (!myReader.HasRows)
            {
                // Do something if the reader doesn't have data. This could be an exception 
                // or other indication to the user that something unexpected happened.
            }
            while (myReader.Read())
            {
                // The exception handler will trigger if the expected fields don't exist 
                // in the returned row
                txtCode.Text = (myReader["EmployeeId"].ToString());
                txtUsername.Text = (myReader["EmpUsername"].ToString());
                txtPass.Text = (myReader["EmpPassword"].ToString());
                txtEmail.Text = (myReader["EmpEmail"].ToString());
                txtFirstname.Text = (myReader["EmpFirstName"].ToString());
                txtLastname.Text = (myReader["EmpLastName"].ToString());
                txtGender.Text = (myReader["EmpGender"].ToString());
                txtContact.Text = (myReader["EmpContact"].ToString());
                txtAddress.Text = (myReader["EmpAddress"].ToString());
                txtDept.Text = (myReader["EmpDept"].ToString());
            }
        }
    }
    // Put a breakpoint on the following curly brace so you can establish that the 
    // code is working without problems (assuming your ducks are otherwise all in a row)
}
catch (Exception ex)
{
    // do something appropriate to indicate whatever problem arises
    // if you're debugging, put a breakpoint on either of the curly braces to stup exceution
}
 
Share this answer
 
v2
Comments
Richard Deeming 12-Feb-18 14:56pm    
"Do it this way instead"
"select * from Employee where EmpUsername='" + Session["id"] + "'"

Noooooo!!!!!!

using (SqlCommand myCommand = new SqlCommand("select * from Employee where EmpUsername = @Id", con){CommandType=CommandType.Text})
{
    myCommand.Parameters.AddWithValue("@Id", Session["id"]);
    ...
}


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
#realJSOP 21-Feb-18 7:22am    
I just copied his code and made a few changes. I was looking at my answer just now, and saw that I ignored that line (I was more concerned with the code's inability to expose the exception he wasn't handling), and was going to fix it, but you covered it. :)

what type of data session["id"] consist?

you are comparing empusername with id.

you have to compare empuserid with id.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900