shalin gajjar
shalin gajjar

Reputation: 684

how to fill grid view with dataset

i have done code to fill up grid view with data set. just look at :

public void FillGrid(string StartAlpha, string CommandName, string ColumnName, string SearchText)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            int userid = db.Users.Where(u => u.EmailAddress.Equals((String)Session["EmailID"])).Select(u => u.Id).SingleOrDefault();
            var sms = Enumerable.Repeat(new
            {
                Id = default(int),
                Title = string.Empty,
                Body = string.Empty,
                FromUser = string.Empty,
                ToUser = string.Empty,
                SentDateTime = default(DateTime?),
                IsMedia = default(bool),
                CreatedDate = default(DateTime),
            }, 1).ToList();
            DataSet myDataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id", typeof(string)));
            dt.Columns.Add(new DataColumn("Title", typeof(string)));
            dt.Columns.Add(new DataColumn("Body", typeof(string)));
            dt.Columns.Add(new DataColumn("FromUser", typeof(string)));
            dt.Columns.Add(new DataColumn("ToUser", typeof(string)));
            dt.Columns.Add(new DataColumn("SentDateTime", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("IsMedia", typeof(bool)));
            dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
            if (StartAlpha.Equals("All"))
            {
                switch (CommandName)
                {
                    case "Inbox":
                        break;
                    case "Outbox":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = true;
                        lbut_showdraffs.Font.Bold = false;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime != null || s.IsDraft.Equals(false)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a=>a.user_id.Equals(userid)).Select(a=>a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                    case "Drafts":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null || s.IsDraft.Equals(true)).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = false;
                        lbut_showdraffs.Font.Bold = true;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime == null || s.IsDraft.Equals(true)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a => a.user_id.Equals(userid)).Select(a => a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                }
            }
            else
            {
                switch (CommandName)
                {
                    //case "Inbox":
                     .............
                    //    break;
                    case "Outbox":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = true;
                        lbut_showdraffs.Font.Bold = false;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime != null || s.IsDraft.Equals(false)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a => a.user_id.Equals(userid)).Select(a => a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList().Where(x => x.Title.StartsWith(StartAlpha, StringComparison.CurrentCultureIgnoreCase)).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                    case "Drafts":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = false;
                        lbut_showdraffs.Font.Bold = true;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime == null || s.IsDraft.Equals(true)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a => a.user_id.Equals(userid)).Select(a => a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList().Where(x => x.Title.StartsWith(StartAlpha, StringComparison.CurrentCultureIgnoreCase)).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                }
            }
            myDataSet.Tables.Add(dt);
            if (myDataSet.Tables[0].Rows.Count > 0)
            {
                DataView myDataView = new DataView();
                myDataView = myDataSet.Tables[0].DefaultView;
                if (this.ViewState["SortExp"] != null)
                {
                    myDataView.Sort = this.ViewState["SortExp"].ToString()
                             + " " + this.ViewState["SortOrder"].ToString();
                }
                GV_ViewSMS.DataSource = myDataView;
            }
            if (GV_ViewSMS.Rows.Count != 0)
            {
                SetPageNumbers();
            }
            GV_ViewSMS.DataBind();
        }
    }

and this error occurs :

Server Error in '/' Application. Column 'SentDatetTime' does not belong to table . Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Column 'SentDatetTime' does not belong to table .

Source Error:


Line 439:                                else
Line 440:                                {
Line 441:                                    dr["SentDatetTime"] = DBNull.Value;
Line 442:                                }
Line 443:                                dr["IsMedia"] = item.IsMedia;


Source File: e:\EASYMAIL_off\EASYMAIL\BulkSMS.aspx.cs    Line: 441

Stack Trace:

[ArgumentException: Column 'SentDatetTime' does not belong to table .]
   System.Data.DataRow.GetDataColumn(String columnName) +5731291
   System.Data.DataRow.set_Item(String columnName, Object value) +13
   BulkSMS.FillGrid(String StartAlpha, String CommandName, String ColumnName, String SearchText) in e:\EASYMAIL_off\EASYMAIL\BulkSMS.aspx.cs:441
   BulkSMS.Page_Load(Object sender, EventArgs e) in e:\EASYMAIL_off\EASYMAIL\BulkSMS.aspx.cs:40
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0 

What wrong with my Code?????

please help me to throughout this problem.....

Upvotes: 0

Views: 357

Answers (1)

Renats Stozkovs
Renats Stozkovs

Reputation: 2605

Looks like you have a spelling error SentDatetTime -> SentDateTime:

if (item.SentDateTime != null)
{
    dr["SentDateTime"] = item.SentDateTime;
}
else
{
    dr["SentDateTime"] = DBNull.Value;
}

Upvotes: 1

Related Questions