2011-12-30 20 views
0

このコードを使用して、SQLデータベースから情報を表示しています。しかし、私はgridviewのデータを見ることができません。どのように私は解決することができますか?また、私はエラーがありません。datagridビューにデータが表示されません

protected void Button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection cnn = new SqlConnection("Server=CAN-PC; Database=SMS; UID=SA; PWD=delidana1963"); 
    string sql = ""; 
    sql = @"select Orginator,RecordDate, (select COUNT(TurkcellID) from SmsStore "; 

// txttarih.Text = Calendar1.SelectedDate.ToString(); 
    var tarih1 = String.Format("{0:yyyy-MM-dd hh:mm}", Calendar1.SelectedDate.Date); 
    var tarih2 = String.Format("{0:yyyy-MM-dd hh:mm}", Calendar2.SelectedDate.Date); 


    if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) 
     sql += "where RecordDate between '" +tarih1 + "' and '" + tarih2 + "'"; 
    else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null) 
     sql += "where RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'"; 
    sql += ") as toplammsj,"; 

    sql += "(select COUNT(TurkcellID) from SmsStore where TurkcellID=1 "; 
    if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) 
     sql += "and RecordDate between '" + tarih1 + "' and '" + tarih2+ "'"; 
    else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null) 
     sql += "and RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'"; 
    sql += ") as giden,"; 

    sql += " (select COUNT(TurkcellID) from SmsStore where TurkcellID=0 "; 
    if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) 
     sql += "and RecordDate between '" + tarih1 + "' and '" + tarih2 + "'"; 
    else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null) 
     sql += "and RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'"; 
    sql += ") as gitmeyen from SmsStore "; 

    if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) 
     sql += "where RecordDate between '" + tarih1+ "' and '" + tarih2 + "'"; 
    else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null) 
     sql += "where RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'"; 

    SqlDataAdapter adp = new SqlDataAdapter(sql, cnn); 
    DataTable dt = new DataTable(); 
    adp.Fill(dt); 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    GridView1.DataMember = "dt"; 
} 

この問題について私を助けてください。 ありがとうございます。

答えて

0

これはWPF GridViewまたはWindows Forms DataGridViewですか? WPF GridViewには、DataSourceプロパティまたはDataBind()メソッドがありません。また、DataGridViewの場合、DataBind()を使用するか、DataSourceがDataTableの場合は、DataMemberプロパティを設定する必要はありません。

また、デバッグの目的で、adp.Fill(dt)の後にブレークポイントを設定します。 dtにカーソルを置き、Rows.Countが0より大きいかどうかを調べるためにプロパティーを調べてください。SQLが乱れていると、データベースのデータが最初から取得されていない可能性があります。

+0

私はブレークポイントを入れ、Rows.Countは6より大きいので、0より大きい。また、私はを使うことができますか? –

関連する問題