Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Error :
Input string was not in a correct format.
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.

C#
Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 

Line 69:         {
Line 70:             path = Server.MapPath("~/Images/");
Line 71:             int Image_Catagory = Convert.ToInt32(DrpImageCatagory.SelectedValue);
Line 72:             int Image_type = Convert.ToInt32(RadioImageType.SelectedValue);
Line 73:             con.Open();

Source File: d:\All Projects Visual Studio\Project_Trial1\Project_Trial1\ImageRep.aspx.cs    Line: 71 

Stack Trace: 

[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +11177559
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145
   System.Convert.ToInt32(String value) +43
   Project_Trial1.ImageRep.BtnSubmit_Click(Object sender, EventArgs e) in d:\All Projects Visual Studio\Project_Trial1\Project_Trial1\ImageRep.aspx.cs:71
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +158
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +174
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +39
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +37
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +105
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4411


code Behind :
C#
private void fillDrpImageCatagory()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Usp_GetImageCatagory", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter Adap = new SqlDataAdapter(cmd);
            dt.Clear();
            Adap.Fill(dt);
            DrpImageCatagory.DataSource = dt;
            DrpImageCatagory.DataTextField = "Descrip";
            DrpImageCatagory.DataValueField = "Id";
            DrpImageCatagory.DataBind();
            DrpImageCatagory.Items.Insert(0, "--Select Catagory--");
            con.Close();
        }

        string ss, path;

        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            ss= Server.MapPath("~/Images/");
            FileUpload1.SaveAs(ss + FileUpload1.FileName);
            Image1.ImageUrl = path;

        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            path = Server.MapPath("~/Images/");
            int Image_Catagory = Convert.ToInt32(DrpImageCatagory.SelectedValue.ToString());
            int Image_type = Convert.ToInt32(RadioImageType.SelectedValue);
            con.Open();

            SqlCommand cmd = new SqlCommand("Insert into ImageRepository1 values('"+ TxtImageName.Text + "','" + TxtImageDescription.Text + "'," + Image_Catagory + "," + Image_type +",'" + path + "')",con);
            int rowcount = cmd.ExecuteNonQuery();
            con.Close();
            if (rowcount > 0)
            {
                LblStatus.Text = "Data Inserted Successful..!";
            }
            else 
            {
                LblStatus.Text = "Something went Wrong !";
            }

database:
SQL
Create table ImageCategory
(Id int Identity(1,1) primary key,
Descrip varchar(50))

Insert into ImageCategory values('Media')
Insert into ImageCategory values('Medical Science')
Insert into ImageCategory values('Nature')
Insert into ImageCategory values('Science')
Insert into ImageCategory values('Sports')
Insert into ImageCategory values('Objects/Items')
Insert into ImageCategory values('Others')



create proc Usp_GetImageCatagory
as
begin
select * from ImageCategory
end
Posted
Updated 15-Aug-19 9:55am
v2
Comments
Andreas Gieriet 7-Dec-14 15:56pm    
What do you see when debugging the offending function?
What is the content of the SelectedValue fields?
Regards
Andi
Satpal Lakhera 8-Dec-14 1:57am    
i've shown that DrpImageCatagory is bind to database table ImageCatagory
and SelectedValue field is bind to its 'Id' which is integer in ImageCatagory as above described.
syed shanu 7-Dec-14 18:59pm    
Hi,
I think you might getting string in Selected value .here you are converting string into the Int data type check the code with break point.
path = Server.MapPath("~/Images/");
Line 71: int Image_Catagory = Convert.ToInt32(DrpImageCatagory.SelectedValue);
Line 72: int Image_type = Convert.ToInt32(RadioImageType.SelectedValue);
Satpal Lakhera 8-Dec-14 1:53am    
i've also tried using:
Line 71: int Image_Catagory = Convert.ToInt32(DrpImageCatagory.SelectedValue);
but it throws same error.(as debuggging says)
syed shanu 8-Dec-14 1:54am    
What value did you get from "DrpImageCatagory.SelectedValue".
I think here is your error check this selected value is returing integer values or any string type value.

1. The exception is generated because you are trying to convert to int a string value that is not an int, in your case may happen that your SelectedValue is undefined (like empty string).

2.This error could be generated by a wrong init of your drop down list control in your code. For example note that your init should be done only once and not in the case of postbacks like in the example below:
C#
protected void Page_Load(object sender, EventArgs e)
       {
               if (!Page.IsPostBack)
               {   
                   //Init your controls only once in this IF block!
                   fillDrpImageCatagory(); 
                   //...
               }
               else
               {
                   //The page was postback, so you could use the user inputs here!
                   //...     
               }
}
 
Share this answer
 
Set a breakpoint in your code on the line marked below:
C#
protected void BtnSubmit_Click(object sender, EventArgs e)
{
    path = Server.MapPath("~/Images/");
    int Image_Catagory = Convert.ToInt32(DrpImageCatagory.SelectedValue.ToString());
    int Image_type = Convert.ToInt32(RadioImageType.SelectedValue);
    con.Open();
    ...

The use the debugger, add a watch and check the contents of DrpImageCatagory.SelectedValue.
It is most likely not an integer.
 
Share this answer
 
As you told every time you get "Select Catagory" in your dropdown boc.

I think you are calling this
C#
fillDrpImageCatagory()

method at page Load .if its so then check for
C#
 if (!IsPostBack)
        {
fillDrpImageCatagory();
}


and also in your button click

C#
  protected void BtnSubmit_Click(object sender, EventArgs e)
        {
here first check for your dropdown box value
if(DrpImageCatagory.Selectedtext =="--Select Catagory--")
{
..... your message
return;
}
            path = Server.MapPath("~/Images/");
            int Image_Catagory = Convert.ToInt32(DrpImageCatagory.SelectedValue.ToString());
            int Image_type = Convert.ToInt32(RadioImageType.SelectedValue);
            con.Open();
}
 
Share this answer
 
Try converting it using Convert.ToInt16() instead?
 
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