Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 

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 7-Oct-11 1:09am
angel 2639
Edited 9-Oct-11 22:03pm
v4
Comments
OriginalGriff at 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.
Indiana.Net at 7-Oct-11 7:11am
   
post your code!
angel 2 at 7-Oct-11 7:52am
   
ive done that
Simon Bang Terkildsen at 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.
Simon Bang Terkildsen at 7-Oct-11 7:38am
   
Great now the code is there, now read Solution 1 by CodeHelper_PS and tell us where the exception is thrown
CodeHelper_PS at 7-Oct-11 7:44am
   
Now tell us where exactly you are getting the exception.
arpitvyas at 7-Nov-12 8:18am
   
ghdfcf
angel 2 at 7-Oct-11 7:48am
   
When I run my program on the localhost and fill in the details, I click the next button and I get an error..
 
"System.FormatException: Input string was not in a correct format."
arpitvyas at 7-Nov-12 8:18am
   
Foket
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v4
Comments
Simon Bang Terkildsen at 7-Oct-11 7:38am
   
My 5, seems the OP didn't read this before add his code dump
CodeHelper_PS at 7-Oct-11 7:52am
   
Thanks simon
angel 2 at 7-Oct-11 7:45am
   
Um still not able to figure out the error!
CodeHelper_PS at 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
CodeHelper_PS at 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
angel 2 at 7-Oct-11 7:52am
   
A 10 digit phone number
A 6 digit postal code
and date in the format 01/01/90
and gender is in the form of a drop down list
angel 2 at 7-Oct-11 8:07am
   
Ive changed that into varchar, but the bug is still there!
CodeHelper_PS at 7-Oct-11 8:13am
   
did you modified in database and code as well or not
angel 2 at 7-Oct-11 8:21am
   
yes I did
angel 2 at 8-Oct-11 1:46am
   
thnks a lot for your help :)
CodeHelper_PS at 8-Oct-11 1:56am
   
Where you got that problem
angel 2 at 8-Oct-11 7:10am
   
In postal code and phone no...Though its running but there's still a problem in the logic...I'l figure that out!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
param[10].Value = "Postalcode";
              param[11].Value = "PhoneNo";
 
as
 
param[10].Value = "12313";
              param[11].Value = "12345";
  Permalink  
v2
Comments
Simon Bang Terkildsen at 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_ssc at 7-Oct-11 7:46am
   
thanks simon
angel 2 at 7-Oct-11 7:54am
   
Ive taken the datatype of date of birth as varchar
hitech_ssc at 7-Oct-11 8:32am
   
ok put breakpoint in button click event you will get where the error is
angel 2 at 8-Oct-11 1:45am
   
hey thanks a lot! :)
hitech_ssc at 8-Oct-11 2:03am
   
where is the problem exactly?
angel 2 at 8-Oct-11 7:10am
   
In postal code and phone no...Though its running but there's still a problem in the logic...I'll figure that out!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

science u have not yet shown the code may be it s postback problem due to view state
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
v2
Comments
angel 2 at 7-Oct-11 8:09am
   
Ive changed that but the error doesnt go!
Simple.v at 7-Oct-11 8:12am
   
Did u changed in database as well as in code?
angel 2 at 7-Oct-11 8:21am
   
yep
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

The only help we can give is to request you post your code.
Do that and help will be more readily forthcoming.
  Permalink  
Comments
angel 2 at 7-Oct-11 7:53am
   
Ive posted my code!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

You have to replace the Postal code and Phone value to INT. Because, while declaring you have declared that as datatype INT.
param[10].Value = 100094;
param[11].Value = 999999999;
  Permalink  
Comments
angel 2 at 8-Oct-11 1:44am
   
hey thank u so much :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Replace this two line
 
param[10] = new SqlParameter("Postalcode", SqlDbType.Int, 100);
param[11] = new SqlParameter("PhoneNo", SqlDbType.Int,100);
 

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

Now your problem is solved.
  Permalink  
Comments
angel 2 at 8-Nov-11 0:01am
   
Ya I did that only...thnks :)
JoseCaniute at 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 at 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
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 202
2 Richard MacCutchan 160
3 PhilLenoir 154
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 5,999
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 7 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100