错误:无法将参数值从String转换为DateTime

时间:2015-09-21 10:32:09

标签: c# asp.net sql-server

我遇到了这样的错误:尝试将复选框列表中的多个选定日期传递给我要在sql中使用的参数时,无法将参数值从String转换为DateTime

我已尝试使用其他数据类型,例如nvarchar,当我将多个选定值传递给1个存储过程参数并使用动态sql返回select语句来填充我的gridview时,它可以正常工作。

聚苯乙烯。在我的网络服务器中,我使用31-Aug-2013在复选框列表中显示为DATE.DataTextFormatString = "{0:dd-MMM-yyyy}";。在我的sql数据库中,它显示为例如2013-08-31

ASPX.CS

    protected void Page_Load(object sender, EventArgs e)
    {
        DATE.DataTextFormatString = "{0:dd-MMM-yyyy}";

        using (SqlConnection conn = new SqlConnection(dbConn))
        {
            try //Call stored procedure
            {

                SqlCommand cmd = new SqlCommand(spddl, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (!IsPostBack)
                {
                    DATE.DataSource = ds.Tables[0];
                    DATE.DataTextField = ds.Tables[0].Columns["DATE"].ToString();
                    DATE.DataBind();



                }
                if (IsPostBack)
                {
                    Bind();
                }    

            }

            catch (Exception i)
            {
                bool exception = true;
                if (exception == true)
                {
                    //txtMessage.Text += e.Message;
                }
            }
        }
    }

public void Bind()
   {
           using (SqlConnection conn = new SqlConnection(dbConn))
       {
           using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
           {
                 String selectedDATE = String.Empty;


                if (DATE.SelectedValue == "All")
                {
                    selectedDATE = "DATE";
                }
                else
                {
                    foreach (ListItem item in DATE.Items)
                    {
                        if (item.Selected)
                        {
                            selectedDATE += "'" + item.Text + "',";
                        }
                    }

                    selectedDATE = selectedDATE.Substring(0, selectedDATE.Length - 1);
                }


               cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param", SqlDbType.DateTime).Value = selectedDATE;


               conn.Open();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds= new DataSet();
               da.Fill(ds);
               GRIDVIEW.DataSource = ds.Tables[0];
               GRIDVIEW.DataBind();

           }
       }

SQL

ALTER PROCEDURE [dbo].[SP]

@param nvarchar(512)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @sql nvarchar(max)
SET @sql = 'SELECT * FROM TABLENAME WHERE [COLUMN] IN (' + @param + ')'
EXEC sp_executesql @sql;
END

1 个答案:

答案 0 :(得分:0)

在SQL方面..
如果[COLUMN]是DateTime类型,则应首先将其转换为

CONVERT(varchar, [COLUMN], 112)

即。你的SP应该像:

ALTER PROCEDURE [dbo].[SP]

@param nvarchar(512)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @sql nvarchar(max)
SET @sql = 'SELECT * FROM TABLENAME WHERE CONVERT(varchar, [COLUMN], 112) IN (' + @param + ')'
EXEC sp_executesql @sql;
END

代码方面..
另一件事是参数,如果您在@param中传递多个日期,则必须在传递之前将其转换为:

foreach (ListItem item in DATE.Items)
{
    if (item.Selected)
    {
        DateTime dtTemp = Convert.ToDateTime(item.Value);
        selectedDATE += "'" + dtTemp.ToString("yyyyMMdd") + "',";
    }
}

selectedDATE = selectedDATE.Substring(0, selectedDATE.Length - 1);