2014-08-31 152 views
0

以下代碼有什麼問題? GridView GridView1根本不在頁面上顯示。GridView不綁定數據源

public void display_range_mark() 
    { 
     int from = int.Parse(ddl_from_mark.SelectedValue.ToString()); 
     int to = int.Parse(ddl_to_mark.SelectedIndex.ToString()); 
     DataTable dt=Data.DoSomthing(string.Format("select ts.Name,ts.FamilyName,ts.Id_student,td.Name,tn.NomreAdad from tblStudent ts,tblDars td,tblNomre tn where tn.NomreAdad>='{0}' AND tn.NomreAdad<='{1}' AND ts.Id=tn.Id_student AND td.Id=tn.Id_dars",from,to)); 
     //DataTable data = Data.DoSomthing(string.Format("select t.Name,t.Id from tblStd t where t.DateSabt='{0}'", p.GetYear(DateTime.Now))); 
     GridView1.DataSource = dt; 
     GridView1.HeaderRow.Cells[0].Text = "نام"; 
     GridView1.HeaderRow.Cells[1].Text = "نام خانوادگی"; 
     GridView1.HeaderRow.Cells[2].Text = "شماره دانش آموزی"; 
     GridView1.HeaderRow.Cells[3].Text = "درس"; 
     GridView1.HeaderRow.Cells[4].Text = "نمره"; 
     GridView1.DataBind(); 



    } 

我收到此錯誤:
異常詳細信息:System.NullReferenceException:未設置爲一個對象的實例對象引用。在這條線發生 錯誤:

GridView1.HeaderRow.Cells[0].Text = "نام"; 

順便說,對於Data.DoSomthing代碼是作爲folllows(它位於類內數據庫):

SqlConnection sc = new SqlConnection(@"Data Source=.;Initial Catalog=School;Integrated Security=True"); 



public DataTable DoSomthing(string text) 
    { 
     sc.Open(); 
     DataTable data = new DataTable(); 
     try 
     { 
      SqlCommand command = new SqlCommand(); 
      command.Connection = sc; 
      command.CommandType = CommandType.Text; 
      command.CommandText = text; 

      SqlDataAdapter sd = new SqlDataAdapter(command); 
      sd.Fill(data); 
      if (data.Rows.Count == 0) 
       data = null; 
     } 
     catch (Exception ex) 
     { 
      sc.Close(); 
      return null; 
     } 
     finally 
     { 
      if (sc.State != ConnectionState.Closed) 
      { 
       sc.Close(); 
      } 
     } 
     return data; 
    } 
+0

你修整調用'GridView1.DataBind();';後'GridView1.DataSource = DT'吧? – 2014-08-31 21:40:10

+0

嘗試在數據綁定後設置單元格文本。或者,也許你應該使用'GridView1.Columns [x] .HeaderText ='''。 – Malk 2014-08-31 22:21:46

+0

@TasosK。是的,我試過了。沒有工作 – JasonStack 2014-09-01 03:46:31

回答

2

在哪裏是連接到數據庫並獲取值的連接對象嗎?

做到這一點的方法:

string query="select ts.Name,ts.FamilyName,ts.Id_student,td.Name,tn.NomreAdad from  tblStudent ts,tblDars  td,tblNomre tn where tn.NomreAdad>[email protected] AND tn.NomreAdad<[email protected] AND ts.Id=tn.Id_student AND td.Id=tn.Id_dars"; 

//Create Sqlconnection object 
using(SqlConnection con = new SqlConnection(connectionstring)) 
{ 
//open the connection 
con.Open(); 
SqlDataAdapter sda = new SqlDataAdapter(query, con); 
//To avoid sql injection using parameters 
sda.Paramaters.AddWithValue("@from",from); 
sda.Paramaters.AddWithValue("@to",to); 
DataTable dt = new DataTable(); 
sda.Fill(dt); 
GridView1.DataSource = dt; 
GridView1.DataBind(); 
} 
+0

一切都很好!它沒有填充數據表,因爲數據庫中沒有匹配,通過改變它的值,它最終能夠工作。而且,正如在註釋中說的那樣,data.DataBind()在列標題分配之前 – JasonStack 2014-09-01 04:34:11

+0

哦很酷,這就是爲什麼我要求你在調試模式下運行,並且添加參數,因爲我添加到命令避免SQL注入。不知道爲什麼它被投票 – Partha 2014-09-01 04:46:45