如何解决错误“输入字符串格式不正确”。 [英] How do I solve the error "Input string was not in a correct format."
问题描述
大家好,我在Visual Studio 2008中的第一个asp.net Web应用程序遇到了一个新问题,错误是
System.FormatException:输入字符串的格式不正确。 pre>。我在网上搜索这个错误,但我还没有得到任何具体的解决方案。所以请帮我弄清楚这个错误。下面是Admin / ManageProducts.aspx和Admin / ManageProducts的代码.aspx.cs文件分别为:
< / head >
< < span class =code-leadattribute> body >
< 表格 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 = < span class =code-keyword> Id >
< 列 >
< asp:CommandField ShowDeleteButton =跨度>True ShowSelectButton = True / >
< asp:BoundField DataField = Id HeaderText = Id SortExpression = Id / >
< asp:BoundField DataField = 名称 HeaderText = 名称 SortExpression = 名称 / >
< asp:BoundField DataField = < span class =code-keyword>价格 HeaderText = 价格 SortExpression = < span class =code-keyword>价格 / >
< asp:BoundField DataField = ImageUrl HeaderText = ImageUrl
SortExpression = ImageUrl / >
< asp:BoundField DataField = 详细信息 HeaderText = 详细信息
< span class =code-attribute> SortExpression = 详细信息 / > ;
< asp:BoundField DataField = ProductCode HeaderText = ProductCode
SortExpression = ProductCode / >
< asp:BoundField DataField = 项目 HeaderText = 项目 SortExpression = 项目 / >
< asp:BoundField DataField = CategoryId HeaderText = CategoryId
< span class =code-attribute> SortExpression = 类别Id / >
< /列 >
< / asp:GridView >
< asp:按钮 ID = Button1 runat = server onclick = Button1_Click
< span class =code-attribute> 文本 = 插入 / >
< asp:ObjectDataSource ID = ObjectDataSourc e2 runat = server
DataObjectTypeName = BusinessObject.Products DeleteMethod < span class =code-keyword> = 删除
SelectMethod = GetAllProductByCategory TypeName = BusinessLogic.ProductsBL >
< SelectParameters < span class =code-keyword>>
< asp:ControlParameter ControlID = DropDownList1 名称 = categoryId
PropertyName = SelectedValue 类型 = Int32 / >
< / SelectParameters >
< / asp:ObjectDataSource >
< asp:DetailsView ID = DetailsView1 runat = < span class =code-keyword> server 高度 = 50px 宽度 = 125px
AutoGenerateRows = False DataSourceID = ObjectDataSource3
oniteminserted = DetailsView1_ItemInserted
oniteminserting = DetailsView1_ItemInserting
< span class =code-attribute> onitemupdated = DetailsView1_ItemUpdated
onitemupdating = DetailsView1_ItemUpdating >
< 字段 >
< asp:BoundField DataField = Id HeaderText = Id SortExpression = Id / >
< asp:BoundField DataField = 名称 HeaderText = < span class =code-keyword>名称 SortExpression = 名称 / >
< asp:BoundField DataField = 价格 HeaderText = 价格 SortExpression = 价格 / >
< asp:BoundField DataField = ImageUrl HeaderText = ImageUrl
< span class =code-attribute> SortExpression = ImageUrl / >
< asp:BoundField DataField = < span class =code-keyword>详细信息 HeaderText = 详细信息
SortExpression = 详细信息 / >
< 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=\"GetDe tail\" 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解决方案Actually, it’s fairly obvious:
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
fuImage.SaveAs(fullFilePath);
Will try to save as that file...
No. I think what you want is this:
string fullFilePath = string.Format(\"{0}/{1}\",absolutePath,fileName);
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 :
</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解决方案Actually, it's fairly obvious:
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
fuImage.SaveAs(fullFilePath);Will try to save as that file...
No. I think what you want is this:
string fullFilePath = string.Format("{0}/{1}",absolutePath,fileName);
这篇关于如何解决错误“输入字符串格式不正确”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!