Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hello everybody, I face a new problem in my first asp.net web application in Visual Studio 2008 and the error is that "
System.FormatException: Input string was not in a correct format.
".I searching on internet about this error but still I didn't got any specific solution.So please help me to figure out this error.Here's below is the code of Admin/ManageProducts.aspx and Admin/ManageProducts.aspx.cs files respectively :

XML
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            DataSourceID="ObjectDataSource1">
        </asp:DropDownList>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            SelectMethod="GetAll" TypeName="BusinessLogic.CategoriesBL"></asp:ObjectDataSource>


        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="ObjectDataSource2" DataKeyNames="Id">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                <asp:BoundField DataField="ImageUrl" HeaderText="ImageUrl"
                    SortExpression="ImageUrl" />
                <asp:BoundField DataField="Detail" HeaderText="Detail"
                    SortExpression="Detail" />
                <asp:BoundField DataField="ProductCode" HeaderText="ProductCode"
                    SortExpression="ProductCode" />
                <asp:BoundField DataField="Item" HeaderText="Item" SortExpression="Item" />
                <asp:BoundField DataField="CategoryId" HeaderText="CategoryId"
                    SortExpression="CategoryId" />
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Insert " />
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
            DataObjectTypeName="BusinessObject.Products" DeleteMethod="Delete"
            SelectMethod="GetAllProductByCategory" TypeName="BusinessLogic.ProductsBL">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="categoryId"
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>

        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
            AutoGenerateRows="False" DataSourceID="ObjectDataSource3"
            oniteminserted="DetailsView1_ItemInserted"
            oniteminserting="DetailsView1_ItemInserting"
            onitemupdated="DetailsView1_ItemUpdated"
            onitemupdating="DetailsView1_ItemUpdating">
            <Fields>
                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                <asp:BoundField DataField="ImageUrl" HeaderText="ImageUrl"
                    SortExpression="ImageUrl" />
                <asp:BoundField DataField="Detail" HeaderText="Detail"
                    SortExpression="Detail" />
                <asp:BoundField DataField="ProductCode" HeaderText="ProductCode"
                    SortExpression="ProductCode" />
                <asp:BoundField DataField="Item" HeaderText="Item" SortExpression="Item" />
                <asp:BoundField DataField="CategoryId" HeaderText="CategoryId"
                    SortExpression="CategoryId" />
                <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:ObjectDataSource ID="ObjectDataSource3" runat="server"
            DataObjectTypeName="BusinessObject.Products" InsertMethod="Insert"
            SelectMethod="GetDetail" TypeName="BusinessLogic.ProductsBL"
            UpdateMethod="Update">
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="Id"
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>
    </div>
    </form>
</body>
</html>




The aspx.cs file here's below:

public partial class Admin_ManageProducts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
    }

    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        FileUpload fuImage = (FileUpload)DetailsView1.FindControl("FileUpload4");

        if (fuImage.HasFile)
        {
            string relativePath = "~/Uploads";
            string absolutePath = Server.MapPath(relativePath);
            string fileName = fuImage.FileName;
            string fullFilePath = string.Format("{0}/{1}",absolutePath,relativePath);
            fuImage.SaveAs(fullFilePath);
            e.Values.Add("ImageUrl", "~/Uploads"+fileName);
        }
    }

    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        FileUpload fuImage = (FileUpload)DetailsView1.FindControl("FileUpload6");
        if (fuImage.HasFile)
        {
            string relativePath = "~/Uploads";
            string absolutePath = Server.MapPath(relativePath);
            string fileName = fuImage.FileName;
            string fullFilePath = string.Format("{0}/{1}",absolutePath,relativePath);
            fuImage.SaveAs(fullFilePath);
            e.NewValues.Add("ImageUrl", "~/Uploads"+fileName);

        }
    }


Please help me
Posted
Comments
George Jonsson 20-Sep-14 4:33am    
On which line do you get the error?
OriginalGriff 20-Sep-14 4:34am    
And where is it complaining?
Which line?
What input?

1 solution

Actually, it's fairly obvious:
C#
string relativePath = "~/Uploads";
string absolutePath = Server.MapPath(relativePath);
string fileName = fuImage.FileName;
string fullFilePath = string.Format("{0}/{1}",absolutePath,relativePath);
fuImage.SaveAs(fullFilePath);


Why?
Assume your website root folder is "C:\Websites\Gharuboy".
So
relativePath = "~/Uploads"
absolutePath = "C:\Websites\Gharuboy\Uploads"
fullFilePath = "C:\Websites\Gharuboy\Uploads/~/Uploads"
And
C#
fuImage.SaveAs(fullFilePath);
Will try to save as that file...
No. I think what you want is this:
C#
string fullFilePath = string.Format("{0}/{1}",absolutePath,fileName);
 
Share this answer
 
Comments
AnoopGharu 20-Sep-14 5:34am    
Thank you sir, Now I got it.Can you tell me please the mean of this following code:
"string fullFilePath = string.Format("{0}/{1}",absolutePath,fileName);"
because I wrote this line from an example.
OriginalGriff 20-Sep-14 6:45am    
Trust me, you can;t just "copy code" from an example and expect it to work: you have to try to understand it first, so it will almost certainly never do what you want!
You should understand this already, it's very basic stuff.
String.Format takes a formatting string (in this case "{0}/{1}") which tells it what to do. In this case, the string means "take the first parameter, add a '/' character, then add the second parameter" and the two partial paths are the parameters.

Does that make sense?
AnoopGharu 22-Sep-14 6:42am    
Thank you sir for your concern, Sir now again I got the above same error that "System.FormatException: Input string was not in a correct format".But in this case the coding of my aspx.cs page was little bit change, but the coding of aspx page is same as above I'd mention.The coding of my aspx.cs page is following:

public partial class Admin_ManageProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
}

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
GridView1.DataBind();
}
}

So please do me favor and help me to fix this error.
Pravuprasad 20-Sep-14 6:01am    
for this refer http://stackoverflow.com/questions/13153417/meaning-of-0-1-in-c-sharp
AnoopGharu 22-Sep-14 7:47am    
By the way, now i got the exact solution of my problem, actually I didn't fill up the data fields at the top of the drop down list and it was in the string format.So that's the main reason I always got this error.
Anyway Thank you everybody for your best concern :)

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