Input string was not in the correct format using int.Parse

Posted by JDWebs on Stack Overflow See other posts from Stack Overflow or by JDWebs
Published on 2012-07-08T08:54:12Z Indexed on 2012/07/08 9:15 UTC
Read the original article Hit count: 270

Filed under:
|
|
|
|

I have recently been making a login 'representation' which is not secure. So before answering, please note I am aware of security risks etc., and this will not be on a live site. Also note I am a beginner :P.

For my login representation, I am using LINQ to compare values of a DDL to select a username and a Textbox to enter a password, when a login button is clicked. However, an error is thrown 'Input string was not in the correct format', when using int.Parse.

Front End:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_Test.aspx.cs" Inherits="Login_Login_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login Test</title>
</head>
<body>
    <form id="LoginTest" runat="server">
    <div>

        <asp:DropDownList ID="DDL_Username" runat="server" Height="20px" 
            DataTextField="txt">
        </asp:DropDownList>
        <br />
        <asp:TextBox ID="TB_Password" runat="server" TextMode="Password"></asp:TextBox>
        <br />
        <asp:Button ID="B_Login" runat="server" onclick="B_Login_Click" Text="Login" />
        <br />
        <asp:Literal ID="LI_Result" runat="server"></asp:Literal>

    </div>
    </form>
</body>
</html>

Back End:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login_Login_Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Binder();
            }
    }

    private void Binder()
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            DDL_Username.DataSource = from x in db.DT_Honeys select new { id = x.UsernameID, txt = x.Username };
            DDL_Username.DataValueField = "id";
            DDL_Username.DataTextField = "txt";
            DDL_Username.DataBind();
        }
    }

    protected void B_Login_Click(object sender, EventArgs e)
    {
        if (TB_Password.Text != "")
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                DT_Honey blah = new DT_Honey();
                blah = db.DT_Honeys.SingleOrDefault(x => x.UsernameID == int.Parse(DDL_Username.SelectedValue.ToString()));
                if (blah == null)
                {
                    LI_Result.Text = "Something went wrong :/";
                }

                if (blah.Password == TB_Password.Text)
                {
                    LI_Result.Text = "Credentials recognised :-)";
                }

                else
                {
                    LI_Result.Text = "Error with credentials :-(";
                }
            }
        }

    }
}

I am aware this problem is very common, but none of the help I have found online is useful/relevant.

Any help/suggestions appreciated; thank you for your time :-).

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about database