Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
A.O.A
hi,
i'm having a problem while checking userid in login table
can anyone tell me what i' doing wrong?
//table
PK	TID	        numeric	9	0
FK	EID	        numeric	9	0
	UserID	        varchar	50	1
	Password	    varchar	16	1
	Verification_key  varchar	50	1
	Verified          varchar	50	1

ASP.NET
//html code
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Panel ID="pnlLogin" runat="server">
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblMessage" Text="" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    User Id :
                </td>
                <td>
                    <asp:TextBox ID="txtUserId" runat="server" Font-Names="Segoe UI" Font-Size="10pt">
                    </asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserIDRequiredFieldValidator" runat="server" ErrorMessage="User ID must be given"
                        ControlToValidate="txtUserId">
                    </asp:RequiredFieldValidator>
                    <cc1:TextBoxWatermarkExtender ID="UserIdTextBoxWatermarkExtender"  runat="server"
                        TargetControlID="txtUserId" WatermarkCssClass="watermark" WatermarkText="UserId">
                    </cc1:TextBoxWatermarkExtender>
                </td>
            </tr>
            <tr>
                <td>
                    Password:
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Font-Names="Segoe UI"
                        Font-Size="10pt">
                    </asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequiredFieldValidator" runat="server" ErrorMessage="Password cannot be empty"
                        ControlToValidate="txtPassword">
                    </asp:RequiredFieldValidator>
                </td>
            </tr>           
            <tr>
                <td colspan="2">
                    <asp:Button ID="cmdLogin" runat="server" Text="Login" OnClick="cmdLogin_Click" />
                </td>
            </tr>
        </table>
    </asp:Panel>

C#
//C# code of login button click

protected void cmdLogin_Click(object sender, EventArgs e)
        {
                int result = 0;
                string sSQL = "", UserID = "", Password = "";

                UserID = txtUserId.Text;
                Password = txtPassword.Text;

                sSQL = "select Tid from employee_login where UserID = '" + UserID + "'  ";
                

//---------------class munshi is defined below
                result = munshi.executeQuery(sSQL);
                if (result > 0)
                {
                    lblMessage.Text = "Welcome :" + UserID ;
                }
                else
                {
                    lblMessage.Text = "User id or password is incorrect!";
                }


}

public class munshi
{
public static int executeQuery(string qry)
    {
        int resultInt = 0;
        string ConnStr = ConfigurationManager.ConnectionStrings["HRFramework"].ConnectionString;
        SqlConnection aConnection = new SqlConnection(ConnStr);
        SqlCommand aCommand = new SqlCommand(qry, aConnection);
        aConnection.Open();
        try
        {
            resultInt = aCommand.ExecuteNonQuery();
        }
        catch (Exception)
        {
            //string ne = exi.Message;
            resultInt = -1;
        }
        finally
        {
            aConnection.Close();
            aCommand.Dispose();
        }

        return resultInt;
    }
}


while debuging.....
C#
resultInt = aCommand.ExecuteNonQuery();

resultint is returning -1,
Posted
Updated 10-Jan-12 0:49am
v3
Comments
Suraj S Koneri 10-Jan-12 6:45am    
Please make your code as readable using C# and html code blocks,
No one can read this and understands to answer.

SQL
select Tid from employee_login where UserID = '" + UserID + "' and password=' " txtpassword.text"'   
";
 
Share this answer
 
Sample code for login button
C#
protected void btnsub_Click(object sender, EventArgs e)
    {


        try
        {
            string str = "select name,responsibility,remark,UserType,Deptt,emailid from empbirth where UserID=@userid and password=@pass";
            SqlCommand cmd = new SqlCommand(str, Db.GetConnection());
            cmd.Parameters.AddWithValue("userid", txtUId.Text);
            cmd.Parameters.AddWithValue("pass", txtpwd.Text);
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {
                Session["name"] = dr["name"].ToString();
                Session["userid"] = txtUId.Text;
                Session["resp"] = dr["responsibility"].ToString();
                Session["remark"] = dr["remark"].ToString();
                Session["tag"] = dr["UserType"].ToString();
                Session["deptt"] = dr["Deptt"].ToString();
                Session["password"] = txtpwd.Text;
                Session["emailid"] = dr["emailid"].ToString();
                cmd.Connection.Close();
              
                Response.Redirect("Home.aspx");
                
            }
            else
            {
               
                Label1.Text = "Wrong Id or Password / Not in DataBase";
                cmd.Connection.Close();
            }

        }


Suggestion:do not use query like
C#
sSQL = "select Tid from employee_login where UserID = '" + UserID + "'  ";
to prevent from SQL Injection.
 
Share this answer
 
v3
Hello,

SqlCommand.ExecuteNonQuery Method

Return Value
Type: System.Int32
The number of rows affected.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.


You may wanna try:
SqlCommand.ExecuteScalar Method
 
Share this answer
 
Comments
zainknoman 10-Jan-12 7:39am    
Thank you

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