如果datetime值为nulll,则获取Datetime值并将其绑定到datagrid中 [英] Get Datetime value and bind it in datagrid if datetime value is nulll

查看:103
本文介绍了如果datetime值为nulll,则获取Datetime值并将其绑定到datagrid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我有从数据库中检索日期的存储过程是SP:-

Hello Friends,

i have store procedure that retrieve date from database here is SP :-

public DataSet GetOrderList(int OrderID, DateTime OrderDate,  string OrderStatus)
    {
        Database db = new SqlDatabase(this.ConnectionString);
        using (DbCommand objcmd = db.GetStoredProcCommand("dbo.GetOrderList"))
        {
            if (OrderID == null)
                db.AddInParameter(objcmd, "@OrderID", DbType.Int32, DBNull.Value);
            else
                db.AddInParameter(objcmd, "@OrderID", DbType.Int32, OrderID);

            if (OrderDate == null)
                db.AddInParameter(objcmd, "@OrderDate", DbType.Date, DBNull.Value);
            else
                db.AddInParameter(objcmd, "@OrderDate", DbType.Date, OrderDate);

            if (OrderStatus == null)
                db.AddInParameter(objcmd, "@OrderStatus", DbType.String, DBNull.Value);
            else
                db.AddInParameter(objcmd, "@OrderStatus", DbType.String, OrderStatus);

            DataSet ds = db.ExecuteDataSet(objcmd);
            return ds;
        }
    }




现在我像这样绑定数据,但:-




now i bind data like that but :-

private void BindData()
    {
        DateTime? dt = null;

        try
        {
            BindData(null, null, 0, null, null);
        }
        catch (Exception ex)
        {
            lblerror.Visible = true;
            lblerror.InnerText = ex.Message.ToString();
        }
    }


private void BindData(string SortExpression, string SortDirection, int OrderID, DateTime OrderDate, string OrderStatus)
    {
            DatabaseHelper db = new DatabaseHelper();
            DataSet ds = db.GetOrderList(OrderID, OrderDate, OrderStatus);

            if (SortDirection != null && SortExpression != null)
            {
                DataView DView = new DataView(ds.Tables[0]);
                DView.Sort = SortExpression + " " + SortDirection;
                GrdOrder.DataSource = DView;
            }
            else
            {
                GrdOrder.DataSource = ds.Tables[0];
                GrdOrder.DataBind();
            }
     }



现在,当datetime为null时,我通过的是..
使用此参数绑定数据的任何其他方法.

谢谢大家



now what i pass when i datetime is null..
Any one other way to bind the data..with this arguments.

Thank You To All

推荐答案

您可以执行以下操作:
You can do something like this:
DateTime dt = DateTime.MinValue;



您不能为 DateTime 设置空值.

希望对您有帮助.



You cannot set null values for DateTime.

Hope it helps.


OP亲自解决了此问题.

OP发表:
在我的代码DateTime中? dt = null;不允许使用空值,这就是问题所在.
OP resolved the issue by himself.

OP posted:
here in my code DateTime? dt = null; not allow null value so that is the problem.


这篇关于如果datetime值为nulll,则获取Datetime值并将其绑定到datagrid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆