2014-02-26 73 views
0

我有一個WinForm C#SQL 2008應用...無法綁定到屬性或列的數據源

我有崩潰的執行,這意味着它拋出一個每日呼叫報告表「不能綁定到該財產或在數據源」異常列Sr_No並指向Program.cs的形式 - >

static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Daily_Call_Report()); 
    } 

我對每日呼叫報告的形式加載事件的一個數據集。下面有它的外觀..

try 
     { 
     SqlConnection last1 = new SqlConnection(@"Data Source=2011-GOA-RCC3\SQLEXPRESS;Initial Catalog=IOB_Comm;Integrated Security=True"); 
     last1.Open(); 
     SqlCommand lasts = new SqlCommand(); 
     lasts.Connection = last1; 
     lasts.CommandText = "Select Top 1 Sr_No from DCR Order By Sr_No Desc "; 
     lasts.ExecuteNonQuery(); 
     SqlDataReader darw = lasts.ExecuteReader(); 
     darw.Read(); 
     label21.Text = darw[@"Sr_No"].ToString(); 

      int last = Convert.ToInt32(label21.Text); 
      int next = last + 1; 
      label21.Text = next.ToString(); 

     darw.Close(); 
     dataGridView2.DataSource = null; 
     //to populate gridview 
     SqlCommand datvi = new SqlCommand(); 
     datvi.Connection = last1; 
     datvi.CommandText = "Select * from DCR"; 
     datvi.ExecuteNonQuery(); 
     SqlDataAdapter swe = new SqlDataAdapter(datvi); 
     DataSet dv1 = new DataSet(); 

     swe.Fill(dv1); 
     dataGridView2.DataSource = dv1.Tables[0]; 

     last1.Close(); 

      //code which allows custom date and time to choose... 
     { 

      dateTimePicker1.Format = DateTimePickerFormat.Custom; 
      dateTimePicker1.CustomFormat = "dd - MM - yyyy HH:mm"; 

      dateTimePicker2.Format = DateTimePickerFormat.Custom; 
      dateTimePicker2.CustomFormat = "dd - MM - yyyy HH:mm"; 

     } 
     } 

上有任何形式的絕對沒有數據綁定控件什麼那麼...

和所有幫助將不勝感激...... 感謝

回答

0

的問題我相信當你期待數據回來時你正在使用ExecuteNonQuery。爲什麼不這樣做呢。

var dataSets = new DataSet(); 
     using (var connx = new SqlConnection(LastUsedConnectionString)) 
     { 
      try 
      { 
       SqlCommand command = new SqlCommand("Select * from DCR", connx); 
       command.CommandType = CommandType.Text; 
       SqlDataAdapter da = new SqlDataAdapter(command); 
       da.Fill(dataSets); 
      } 
      finally 
      { 
       connx.Close(); 
      } 
     } 
相關問題