Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi this is my code..but am getting error..can any one tel me what s the error in my code..thanks

   private User GetUserInfo(string userId)
    {
        string sqlConnection = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
        SqlConnection conn;
        SqlCommand cmd;
        User lst = new User();

        try
        {
            using (conn = new SqlConnection(sqlConnection))
            {
                conn.Open();

                StringBuilder sbQry = new StringBuilder();
//begin..in this line am getting error
                sbQry.Append("select * FROM User where UserId = " + userId);
//end
                using (cmd = new SqlCommand(sbQry.ToString(), conn))
                {
                    cmd.CommandType = CommandType.Text;

                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            User temp = new User();
                            temp.UserID = Tools.Tools.IifInt(dr["UserID"]);
                            temp.Username = Tools.Tools.IifStr(dr["Username"]);
                            temp.Password = Tools.Tools.IifStr(dr["Password"]);
                            temp.LastName = Tools.Tools.IifStr(dr["LastName"]);
                            temp.FirstName = Tools.Tools.IifStr(dr["FirstName"]);
                            temp.MiddleName = Tools.Tools.IifStr(dr["MiddleName"]);
                            temp.WorksiteCode = Tools.Tools.IifStr(dr["WorksiteCode"]);
                            temp.AccessLevel = Tools.Tools.IifInt(dr["AccessLevel"]);
                            temp.Active = Tools.Tools.IifStr(dr["Active"]);
                            temp.DateCreated = Tools.Tools.IifDT(dr["DateCreated"]);
                            //temp.DateUpdated = Tools.Tools.IifDate(dr["DateUpdated"]);
                            temp.WorksiteCode = Tools.Tools.IifStr(dr["Worksitedesc"]);
                            lst = temp;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lst.Username = "Error in retrieving record";
        }
        return lst;
    }


error is invalid syntax near User...
can any one suggest me?
Posted

This is vulnerable to SQL injection. Read here http://www.unixwiz.net/techtips/sql-injection.html[^]

Rather use parameters:

C#
SqlCommand sql = new SqlCommand("SELECT * FROM Users WHERE UserID = @userID", sqlConnection);

sql.Parameters.Add(new SqlParameter("userID", userId);

sql.ExecuteReader();


That should work and will be exponentially more secure ;)

-Dom
 
Share this answer
 
The code you have shown does not give the error you describe.

If I copy it into one of my files, I get the kind of errors I would expect ("User" class doesn't exist, User.UserId doesn't exist, that sort of thing)

But the line you are reporting as having the error doesn't report any error. Go back to your compiler, do a clean re-build, then double click on the error message to take you to the line. I don't think it is where you think it is.
 
Share this answer
 
Comments
BalaThakur 9-May-12 7:04am    
You may have used another table name than "User". Thats y...
OriginalGriff 9-May-12 7:11am    
No, he is reporting the error as:
"error is invalid syntax near User..."
on the line:
//begin..in this line am getting error
sbQry.Append("select * FROM User where UserId = " + userId);
//end

There are quite a few reasons why that will cause SQL exceptions, but they will occur further on in the code.
ythisbug 9-May-12 7:22am    
exactly but i changed to tblUser. so now working thanks
Shahin Khorshidnia 9-May-12 13:58pm    
+5
Hello...

1 ) User is a reserved word in SQL server .. you can rename the table or write [User]
2 ) You should use stored proc

bye
 
Share this answer
 
C#
sqldataadapter ad=new sqldataadapter("string s=select * from table where userid='"+txtuname.text+"'",connection object);
dataset ds=new dataset();
ad.fill(Ds);

if(Ds.table[0].rows.count!=0)
{
          means record found
}
else
{
        means record not found
}
 
Share this answer
 
v4
Replace with following,

sbQry.Append("select * FROM User where UserId = '" + userId+"'");
 
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