Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnSubmit_Click(object sender, EventArgs e)
    {
       SqlDataReader sdrDatanew = null; //Read rows one by one.
        string strnew;
        string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString"].ConnectionString; //Define new connectionstring
        SqlConnection connew = new SqlConnection(connectionString); //establishes a new sql connection
        connew.Open(); //Opens Connection
        strnew = "select User_Type from User_Details where User_Type='" + ddlUserSel.SelectedItem.Value + "' AND LoginID = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'";
        SqlCommand sqlCommnew = new SqlCommand(strnew, connew); //passes the command and connection
        sdrDatanew = sqlCommnew.ExecuteReader(); //For select command

        int userType = 0;
        
        if (sdrDatanew.HasRows)
        {
            if (sdrDatanew.Read())
            {
                userType = Convert.ToInt32(sdrDatanew["User_Type"].ToString());
           }
        }

switch (userType)
{
    case 0:
        Response.Redirect("Lic_Gen.aspx");
        break;
    case 1:
        Response.Redirect("Cust_Page.aspx");
        break;
    default:
        lblDisp.Text= "Invalid User/Password";
        break;
}

connew.Close();
    }


I'm having issues in redirecting my page. Irrespective of wrong password or user id, or even choosing customer or anything, for everything, the page gets redirected to the "Lic_gen.aspx" page. I know i've explicitly defined "usertype" as 0, but I'm not really getting how to resolve this error.

Can someone help me please.
Posted
Comments
[no name] 17-Dec-12 4:58am    
I have some question for you...

1. How many records are there in your database for a given credentials?
2. Check the user type of the record that is returning from the database.

once try like Response.Redirect("Lic_Gen.aspx", false); and let me know whether this works or not.
BlehBlahBah 17-Dec-12 5:03am    
This is causing an issue:

1. No data returned in sdrDatanew, hence the userType always remains 0
2. Data from DB returned under sdrDatanew["User_Type"] is always 0 (string), hence the userType always remains 0

Hi,

Please optimise your select query like

select User_Type from User_Details where LoginID = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'"


And try again.

Thanks
 
Share this answer
 
Comments
BlehBlahBah 17-Dec-12 5:38am    
Still redirects to "lic_gen.aspx" page.
[no name] 17-Dec-12 6:33am    
Check the logintype value in the database for the given credentials whether that contains any value or not.
I am not sure, may be your query is not returning rows. and hence you have initialized userType=0, its going to the case 0:

So, try debugging your code.
 
Share this answer
 
Comments
BlehBlahBah 17-Dec-12 5:18am    
This is causing an issue:

1. No data returned in sdrDatanew, hence the userType always remains 0
2. Data from DB returned under sdrDatanew["User_Type"] is always 0 (string), hence the userType always remains 0
This is because userType variable is initialized to value 0. I am not sure what all other values sdrDatanew["User_Type"] returns.
You can initialize userType value initially to some other value like Int32.MinValue and then try out.
 
Share this answer
 
v2
Comments
BlehBlahBah 17-Dec-12 5:04am    
Can you help me a little bit with that? i'm still new to this language. and this problem is eating me out.
Kiran Susarla 17-Dec-12 5:33am    
Initialize userType to Int.MinValue instead of 0, like
int userType = Int.MinValue;

So now when datareader doesn't return any rows your userType variable will have Int.MinValue and swtich statement will execute default case.
Hope I am clear.
BlehBlahBah 17-Dec-12 5:37am    
what is Int in Int.MinValue?
Kiran Susarla 17-Dec-12 5:41am    
My bad. It is Int32.MinValue (http://msdn.microsoft.com/en-us/library/system.int32.minvalue(VS.100).aspx).
I have updated this in the solution as well.
BlehBlahBah 17-Dec-12 5:43am    
Thanks a lot kiran! You're my saviour! :D

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