[請益] 關於C#用Calender做查詢
各位好
小弟現在的作業環境為C#的webform
做一個查詢的功能,利用Calender所select的值與資料庫裡的Date資料做比對~如果查不
到或是空值~就會顯示"查無資料"
現在寫了一小段~但是出來的結果是
{
例外詳細資訊: System.NullReferenceException: 並未將物件參考設定為物件的執行個
體
原始程式錯誤:
行 51: GridView1.DataBind();
行 52: }
行 53: GridView1.HeaderRow.Cells[1].Text = "消息編號";
行 54: GridView1.HeaderRow.Cells[2].Text = "消息名稱";
行 55: GridView1.HeaderRow.Cells[3].Text = "消息日期";
}
我寫的語法如下
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string connstr =
ConfigurationManager.ConnectionStrings["Erlin"].ConnectionString;
string sqlstr = "Select NewsID,NewsName, NewsDate From News where
NewsDate = '" + Calendar1.SelectedDate.Date.ToShortDateString() + "' order by
NewsID DESC" ;
OleDbDataAdapter adpt = new OleDbDataAdapter(sqlstr, connstr);
DataSet ds = new DataSet();
adpt.Fill(ds);
if (Calendar1.SelectedDate.Date.ToShortDateString() != "NewsDate")
{
Label2.Text = "查無消息資料";
}
else
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
GridView1.HeaderRow.Cells[1].Text = "消息編號";
GridView1.HeaderRow.Cells[2].Text = "消息名稱";
GridView1.HeaderRow.Cells[3].Text = "消息日期";
}
有勞各位了...感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.221.176
→
08/14 23:08, , 1F
08/14 23:08, 1F
Programming 近期熱門文章
PTT數位生活區 即時熱門文章