Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
i am making a login page and it is working but once i open first time then i am getting a message " INPUT STRING WAS NOT IN CRRECT FORMAT"
PLEASE check and guide me where i am wrong



C#
private void button4_Click(object sender, EventArgs e)
{
    Form1 fr = new Form1();
    fr.Refresh();
    try
    {
        con = new SqlConnection("Data Source=.;Initial Catalog=DSIIDC2;Integrated Security=True");
        cmd = new SqlCommand("select * from login where username='" + txtusername.Text + "' and password='" + txtpassword.Text + "' and role='admin01'", con);
       // cmd = new SqlCommand("select username='" + txtusername.Text + "', password='" + txtpassword.Text + "' from login where role='admin01'");
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable datatab = new DataTable();
        da.Fill(datatab);
        if ("admin" == txtusername.Text && "dsiidc" == txtpassword.Text && "admin01" == "admin01")
        {
            //Session["userone"] = TextBox1.Text;

            frm2.Show();


        }
        else if (("admin" == txtusername.Text && "admin" == txtpassword.Text && "admin02" == "admin02"))
        {

            ad.Show();
            da.Dispose();

        }
        else
        {
            txtpassword.Text = "";
            txtpassword.Text = "";


        }

    }

    catch (Exception ex)
    {
        MessageBox.Show("Please Enter User name and password");
    }


}
Posted
Updated 10-Jun-15 20:59pm
v2
Comments
King Fisher 11-Jun-15 3:01am    
So, whats your Input Data?
Richard MacCutchan 11-Jun-15 3:53am    
Just about everything is wrong with that code. You are storing the passwords in clear text. And you are using string concatenation which can lead to the destruction of your database.
Dave Kreskowiak 11-Jun-15 8:52am    
virtual +5
José Amílcar Casimiro 11-Jun-15 4:37am    
Your code is garbage, you send out.

1 solution

If you debug the code you'll see which line of code is causing the error. Then you'll see what the problem is with the string. However, I don't see anything in the code you have posted that would cause that error so again, pay attention to where the error tells you it is happening.

Then you'll also want to change your code to use parameters.

For example:
C#
 cmd = new SqlCommand("select * from login where username=@userName and password=@password and role='admin01'", con);
cmd.Parameters.AddWithValue("@userName", txtusername.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
 
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