You must Sign In to post a response.
  • Category: .NET

    How to show a message in label if entered values are incorrect

    below is my code where ia m wrong i cant understanding actual problem is if i enter employee id and password and select hr its redirecting to next page but when selected employee it should display in label "incorrect id and pass word" but it showing like below and i have written code below once check where iam wrong
    ""Conversion failed when converting the varchar value 'ravi' to data type int.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting the varchar value 'ravi' to data type int.

    Source Error:


    Line 51: cmd.Connection = con;
    Line 52:
    Line 53: int OBJ = Convert.ToInt32(cmd.ExecuteScalar());
    Line 54:
    Line 55: if (OBJ > 0)

    code::::::::::::::::::::::::::::::::::::::

    Session["uname"] = TextBox1.Text;


    if (DropDownList1.SelectedItem.Text=="HR" && TextBox1.Text == "ravi" && TextBox2.Text == "123456")
    {
    Response.Redirect("HR.aspx");
    }
    else{

    Label1.Text="please enter correct ID and Password";
    Label1.Visible = true;

    }


    if (DropDownList1.SelectedItem.Text == "EMPLOYEE")
    {
    SqlConnection con = new SqlConnection(@"Data Source=ADMIN-PC;Initial Catalog=Registration;Integrated Security=True");

    con.Open();

    SqlCommand cmd = new SqlCommand("select * FROM ekthaemployeeab WHERE EmployeeId ='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'");

    cmd.Connection = con;

    int OBJ = Convert.ToInt32(cmd.ExecuteNonQuery());

    if (OBJ > 0)
    {
    Session["name"] = TextBox1.Text;

    Response.RedirectPermanent("employeea.aspx");

    }

    else
    {

    Label1.Text = "please enter correct ID and Password";


    }}
  • #767859
    I dont understand your code.

    You have mentioned TextBox1.Text = "Ravi" then in SQL you mapping that to Employee ID?
    Employee ID might be Integer but you are mapping that to String.

    Thanks,
    Mani

  • #767861

    Hi Ravi.

    As per your error message it is clear that you are trying to pass varchar to the int column in database.
    Make sure the datatype of sending value and the receiving value(at DB side) has same datatype.
    I suggest you not to use direct if condition with static values like if (DropDownList1.SelectedItem.Text=="HR" && TextBox1.Text == "ravi" && TextBox2.Text == "123456").
    Avoid this and check it in query like below.

    SqlCommand cmd = new SqlCommand("select * FROM ekthaemployeeab WHERE designation="'+DropDownList1.SelectedItem.Text+"' and EmployeeId ='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'");

    Last thing is why you used Response.RedirectPermanent("employee.aspx); instead of that use Response.Redirect("employee.aspx");


    Sridhar Thota.
    Editor: DNS Forum.

  • #767875
    Hi,

    Problem on this line


    SqlCommand cmd = new SqlCommand("select * FROM ekthaemployeeab WHERE EmployeeId ='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'");


    when you select ravi in textbox1 and trying to fetch it from database the database field is integer type (EmployeeId), so while mapping you have to choose the appropriate database field other wise you will get this type of error message only.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767887
    its a column data type mismatch error, check below query to correct your issue

    SqlCommand cmd = new SqlCommand("select * FROM ekthaemployeeab WHERE designation="'+DropDownList1.SelectedItem.Text+"' and EmployeeId ='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'");

    hope it helps to resolve your issue

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767914
    Hai Ravi Raman,
    You need to use Try--Catch block for showing the error message in the label when appeared:

    try
    {
    if (DropDownList1.SelectedItem.Text=="HR" && TextBox1.Text == "ravi" && TextBox2.Text == "123456")
    {
    Response.Redirect("HR.aspx");
    }
    }
    catch(Exception ex)
    {
    Label1.Text="please enter correct ID and Password";
    Label1.Visible = true;
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments