【问题标题】:Conversion failed when converting the varchar value '{0}' to data type int将 varchar 值“{0}”转换为数据类型 int 时转换失败
【发布时间】:2011-11-10 17:22:33
【问题描述】:

我在尝试执行 gridview 行创建事件时出错

<asp:GridView ID="grdDetailedMes" runat="server" Width="800px"
                        ForeColor="black" GridLines="Horizontal" CellPadding="6" BackColor="white"
                        BorderColor="Black" BorderStyle="Solid" ShowHeader="False" OnRowCreated="grdDetailedMes_rowcreated">                      

                    </asp:GridView>

在.cs文件中

SqlConnection cnn = new SqlConnection("Data Source=ITS-BA-DC02\\MSSQL2008_SAND;Initial Catalog=TestDB_Chaitanya;User Id=sa;Password=01Explore");
        try
        {
            cnn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select MesDetails from InboxMesgDetails where InboxMesId='{0}'", cnn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            grdDetailedMes.DataSource = ds;
            grdDetailedMes.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            cnn.Close();
        }

protected void grdDetailedMes_rowcreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView headergrid = (GridView)sender;
            GridViewRow Headergridrow = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Insert);

            TableCell Headercell = new TableCell();
            Headercell.Text = "Company";

            Headercell.ColumnSpan = '2';
            Headergridrow.Cells.Add(Headercell);
        }
    }

【问题讨论】:

  • 欢迎来到 SO。请注意,使用{}(工具栏按钮)放置您的代码部分。

标签: asp.net sql sql-server


【解决方案1】:

你的错误在这里:

“从 InboxMesgDetails 中选择 MesDetails 其中 InboxMesId='{0}'”

您是否打算对此进行 string.format 处理?您目前没有用任何东西替换该占位符。现在它正在尝试使用文字 {0}。此外,应该不需要单引号,因为看起来您的 InboxMesId 是一个 int

【讨论】:

    【解决方案2】:

    您是否正在寻找替换 {0} 中的内容? 如果是,则您的代码不会处理此问题。

    SQL 无法将 { } 转换为 int。

    【讨论】:

      【解决方案3】:

      您没有在此处为您的查询传递 {0} 的参数:-

                  SqlDataAdapter da = new SqlDataAdapter("select MesDetails from InboxMesgDetails where InboxMesId='{0}'", cnn);
      

      您应该使用 '@Param' 代替 {0} 并且应该使用 SqlCommand.Parameters Property 传递一个有效参数

      你可以这样做-

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
      
            String commandText="select MesDetails from InboxMesgDetails where InboxMesId=@ID";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Parameters.Add("@ID", SqlDbType.Int);
            command.Parameters["@ID"].Value = InboxMesId;
            command.Parameters.Add("@ID", SqlDbType.Int);
            try
            {
                connection.Open();
                int rowsAffected = command.ExecuteNonQuery();
                Console.WriteLine("RowsAffected: {0}", rowsAffected);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      
       }
      

      【讨论】:

        【解决方案4】:

        这应该对你有帮助SqlDataAdapter

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-01
          • 2012-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多