【问题标题】:Error "Input string was not in a correct format"错误“输入字符串的格式不正确”
【发布时间】: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 LinkBut​​ton).CommandArgument);

标签: c# asp.net gridview


【解决方案1】:

来自命令的值类似于“uc3f13se”。然后你试图把它解析成一个整数。该值不是整数,因此 int.Parse() 将失败。

int class_id = int.Parse((sender as LinkButton).CommandArgument);

您可能应该只将它用作字符串,CommandArgument 很可能是已经给出您给我的示例值的字符串类型:

var class_id = ((LinkButton)sender).CommandArgument;

这当然是假设您的数据库中的 class_id 列是 varchar 或其他接受字符串的类型。

如果您对 int.Parse() 有更多疑问,可以访问 here,它会抛出哪些类型的异常以及如何导致它们。

【讨论】:

  • 它可以工作,但现在我得到了这个错误 ExecuteReader: CommandText property has not been initialized in the same code
  • cmd.Connection = con; con.Open();使用 (SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read();字节 = (byte[])sdr["filePath"]; contentType = sdr["fileType"].ToString();文件名 = sdr["文件名"].ToString(); } con.Close();
  • 这似乎是一个新问题。我提供的答案回答了你原来的问题。我建议将此标记为答案,然后尝试找出下一部分。如果仍然失败,请发布新问题。
  • 我确实知道您的问题是什么,您正在创建一个与您正在生成的 SqlConnection 无关的命令。这里的提示是 SqlCommand 对象有一个 CreateCommand 方法。从这里开始:msdn.microsoft.com/en-us/library/…SqlCommand command=connection.CreateCommand();
  • SELECT [class_id], [module_id], [lecturer_id], [type], [start_date], [end_date], [name], [filePath] FROM [Lecturer] FROM [ClassAssignmentModule]";为什么会有2个FROM语句?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 2018-04-07
  • 1970-01-01
相关资源
最近更新 更多