【发布时间】:2014-09-05 04:13:28
【问题描述】:
我正在使用 asp.net、c# 和 SQL Server 开发一个 Web 应用程序项目。上传过程运行良好,但下载过程出现此错误“输入字符串格式不正确”。表中的列 class_id 是 varchar,我将其用作 gridview 中的 DataKeyNames
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
CommandArgument='<%# Eval("class_id") %>'></asp:LinkButton>
当点击gridview中的下载按钮时,以下代码用于下载文件
protected void DownloadFile(object sender, EventArgs e)
{
int class_id = int.Parse((sender as LinkButton).CommandArgument); // the error is here
byte[] bytes;
string fileName, contentType;
string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())//Error is here
{
sdr.Read();
bytes = (byte[])sdr["filePath"];
contentType = sdr["fileType"].ToString();
fileName = sdr["fileName"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
如何解决这个错误。 在此先感谢
【问题讨论】:
-
哪一行给出了这个错误?有趣,虽然它可能太猜测它可能最容易告诉我们。
-
您是否在该行设置了断点,以检查发送方对象的 CommandArgument 属性中是否发送了任何值?
-
WHERE [class_id] + [module_id] LIKE '%" + para.Text.Trim() + "%'?这导致了您的问题,但您根本不应该连接字符串来创建 sql 字符串。而是使用参数。 -
抱歉,此代码已更正,因此它用于搜索文件详细信息。我刚刚从 [Lecturer] 中删除
-
是的,我做了beakpoint,class_is以字符串格式显示..这一行中的错误 int class_id = int.Parse((sender as LinkButton).CommandArgument);