Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
Ive made a registration form(using asp.net and C#) with some attributes like username, password,date of birth,phone no,address, gender, postal code etc. There's no error in the code.
But When I run my web site and fill in the details, and press the OK button, I get an error:"Input string was not in a correct format."
Plz help me!

the code is:


C#
private void ExecuteInsert(string Username, string Password, string Confirmpassword, string EmailID, string Firstname, string Lastname, string Address, string City,string State, string Country, string Postalcode,string PhoneNo, string gender, string DOB)
        {
       
            SqlConnection con = new SqlConnection(" Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Online job portal\\App_Data\\database.mdf;Integrated Security=True;User Instance=True");

            string sql = "INSERT INTO ojb1(Username,Password,ConfirmPassword,EmailID,Firstname,Lastname,Address,City,State,Country,Postalcode,PhoneNo,Gender,DOB) VALUES" + "(@Username,@Password,@ConfirmPassword,@EmailID,@Firstname,@Lastname,@Address,@City,@State,@Country,@Postalcode,@PhoneNo,@Gender,@DOB)";

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlParameter[] param = new SqlParameter[14];

                param[0] = new SqlParameter("@Username", SqlDbType.VarChar, 50);
                param[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
                param[2] = new SqlParameter("@ConfirmPassword", SqlDbType.VarChar, 50);
                param[3] = new SqlParameter("@EmailID", SqlDbType.VarChar, 50);
                param[4] = new SqlParameter("Firstname", SqlDbType.VarChar, 50);
                param[5] = new SqlParameter("Lastname", SqlDbType.VarChar, 50);
                param[6] = new SqlParameter("Address", SqlDbType.VarChar, 50);
                param[7] = new SqlParameter("City", SqlDbType.VarChar, 50);
                param[8] = new SqlParameter("State", SqlDbType.VarChar, 50);
                param[9] = new SqlParameter("Country", SqlDbType.VarChar, 50);
                param[10] = new SqlParameter("Postalcode", SqlDbType.Int, 100);
                param[11] = new SqlParameter("PhoneNo", SqlDbType.Int,100);
                param[12] = new SqlParameter("Gender", SqlDbType.Char, 10);
                param[13] = new SqlParameter("DOB", SqlDbType.VarChar, 50);


                param[0].Value = Username;
                param[1].Value = Password;
                param[2].Value = Confirmpassword;
                param[3].Value = EmailID;
                param[4].Value = Firstname;
                param[5].Value = Lastname;
                param[6].Value = Address;
                param[7].Value = City;
                param[8].Value = State;
                param[9].Value = Country;
                param[10].Value = Postalcode;
                param[11].Value = PhoneNo;
                param[12].Value = gender;
                param[13].Value = DOB;

                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }

            catch (SqlException ex)
            {
                string msg = "Insert Error";
                msg += ex.Message;
                throw new Exception(msg);

            }
            finally
            {
                con.Close();
            }
           
        }
        
    
     
    
 public static void ClearControls(Control Parent)

    {

        if (Parent is TextBox)

        { (Parent as TextBox).Text = string.Empty; }

        else

        {

            foreach (Control c in Parent.Controls)

                ClearControls(c);

        }

    }
    protected void Next1_Click(object sender, EventArgs e)
    {
        if (password.Text == confirmpass.Text)
        {
            ExecuteInsert(username.Text, password.Text, confirmpass.Text, EmailID.Text, firstname.Text, Lastname.Text, Address.Text, City.Text, State.Text, Country.Text,Postalcode.Text,Phoneno.Text,Gender.SelectedItem.Text,DOB.Text);
            Response.Write("Record was successfully added");
            ClearControls(Page);
        }
        else
        {
            Response.Write("password did not match");
            password.Focus();
        }
    }
Posted
Updated 12-Jan-23 19:38pm
v4
Comments
OriginalGriff 7-Oct-11 7:11am    
Without the code fragment that generates the error, we would be just guessing!
Use the "Improve question" widget to edit your question and provide better information.
Member 11722041 18-Jun-15 9:07am    
tyjtyjt
choudhary.sumit 7-Oct-11 7:11am    
post your code!
angel 2 7-Oct-11 7:52am    
ive done that
Simon Bang Terkildsen 7-Oct-11 7:12am    
There's no error in the code
Yes there is otherwise you wouldn't get an exception, now would you?!
Post the relevant code.

No one is able to tell you where exactly you are getting error with out having the knowledge about the code written by you.
so Place break point in Ok button click event in your code and see where you are getting that error.

change datatype of phone number from int to varchar and then check once.

Then the problem is probably with phone number because
datatype int can not hold 10 digit phone numbers.
because the limit of int datatype is

Allows whole numbers between -2,147,483,648 and 2,147,483,647
 
Share this answer
 
v4
Comments
Simon Bang Terkildsen 7-Oct-11 7:38am    
My 5, seems the OP didn't read this before add his code dump
P.Salini 7-Oct-11 7:52am    
Thanks simon
angel 2 7-Oct-11 7:45am    
Um still not able to figure out the error!
P.Salini 7-Oct-11 7:48am    
May be you are facing problem with either dateofbirth or with gender or with phone number
what are your inputs there
check whether you are entering correct values or not
P.Salini 7-Oct-11 7:50am    
I think you are facing problem with phone number
declare phone number as varchar instead of int.
It may solve your problem
science u have not yet shown the code may be it s postback problem due to view state
 
Share this answer
 
I think the problem at phone number.

The integer range in sql server is

-2,147,483,648 and 2,147,483,647
so change the data type of ur ph num from int to varchar and try again
 
Share this answer
 
v2
Comments
angel 2 7-Oct-11 8:09am    
Ive changed that but the error doesnt go!
sravani.v 7-Oct-11 8:12am    
Did u changed in database as well as in code?
angel 2 7-Oct-11 8:21am    
yep
i think in your database the datatype of date of birth is datetime.

while you are inserting record you are taking date of birth as text.

to rectify this you need to convert your string format of date to date like this

Convert.ToDateTime(dateofbirthtextbox.text)




if you are still facing the problem

replace these two lines
C#
param[10].Value = "Postalcode";
              param[11].Value = "PhoneNo";


as

C#
param[10].Value = "12313";
              param[11].Value = "12345";
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 7-Oct-11 7:41am    
Valid guess, my 5, however Convert.ToDateTime can cast an exception as well, DateTime.TryParse would be the method to use.
hitech_s 7-Oct-11 7:46am    
thanks simon
angel 2 7-Oct-11 7:54am    
Ive taken the datatype of date of birth as varchar
hitech_s 7-Oct-11 8:32am    
ok put breakpoint in button click event you will get where the error is
angel 2 8-Oct-11 1:45am    
hey thanks a lot! :)
The only help we can give is to request you post your code.
Do that and help will be more readily forthcoming.
 
Share this answer
 
Comments
angel 2 7-Oct-11 7:53am    
Ive posted my code!
You have to replace the Postal code and Phone value to INT. Because, while declaring you have declared that as datatype INT.
C#
param[10].Value = 100094;
param[11].Value = 999999999;
 
Share this answer
 
Comments
angel 2 8-Oct-11 1:44am    
hey thank u so much :)
Replace this two line

C#
param[10] = new SqlParameter("Postalcode", SqlDbType.Int, 100);
param[11] = new SqlParameter("PhoneNo", SqlDbType.Int,100);



to

C#
param[10] = new SqlParameter("Postalcode",  SqlDbType.VarChar, 100);
param[11] = new SqlParameter("PhoneNo", SqlDbType.VarChar,100);



Now your problem is solved.
 
Share this answer
 
Comments
angel 2 8-Nov-11 0:01am    
Ya I did that only...thnks :)
JoseCaniute 1-Feb-12 7:01am    
int curr_bal = int.Parse(cmd.ExecuteScalar().ToString());

whn i started to execute the error will occur in this line.
The Error is:Input string was not in a correct format.
JoseCaniute 1-Feb-12 7:02am    
Pls Some one help me to knw abt the error.

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900