2014-04-12 3 views
1

누구든지이 코드를 도와 줄 수 있습니까? 레코드를 업데이트하려고하는데이 줄에 The DataAdapter.SelectCommand property needs to be initialized.이 오류가 발생합니다. data_adapter.UpdateCommand = builder.GetUpdateCommand(); 수정 방법을 알 수 없습니다.ADOBE 테이블의 레코드를 업데이트 할 때의 문제

public partial class AirlineReservation : Form 
    { 
     public SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog=Airline_Reservation; Integrated Security=SSPI"); 
     public SqlDataAdapter data_adapter = new SqlDataAdapter(); 
     public DataSet ds = new DataSet(); 
     public DataGridView parentDataGridView = new DataGridView(); 
     public BindingSource parentBindingSource = new BindingSource(); 
     public DataGridView childDataGridView = new DataGridView(); 
     public BindingSource childBindingSource = new BindingSource(); 

     SqlCommand select_planes = new SqlCommand(); 
     SqlCommand select_airlines = new SqlCommand(); 
     public SqlCommandBuilder builder; 

     public DataRelation rel; 

     public AirlineReservation() 
     { 
      InitializeComponent();   
     } 

     private void getData() 
     { 
      SqlDataAdapter parentDataAdapter = new SqlDataAdapter("select * from Airline", connection); 
      parentDataAdapter.Fill(ds, "Airline"); 
      SqlDataAdapter childDataAdapter = new SqlDataAdapter("select * from Plane", connection); 
      childDataAdapter.Fill(ds, "Plane"); 

      DataColumn parentColumn = ds.Tables["Airline"].Columns["airline_id"]; 
      DataColumn childColumn = ds.Tables["Plane"].Columns["airline_id"]; 

      rel = new DataRelation("PlaneAirline", parentColumn, childColumn); 
      ds.Relations.Add(rel); 

      parentBindingSource.DataSource = ds; 
      parentBindingSource.DataMember = "Airline"; 
      childBindingSource.DataSource = parentBindingSource; 
      childBindingSource.DataMember = "PlaneAirline"; 
     } 

     private void AirlineReservation_Load(object sender, EventArgs e) 
     { 
      parentDataGridView.DataSource = parentBindingSource; 
      childDataGridView.DataSource = childBindingSource; 
      getData(); 
     } 

     private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 

      DataView dv = new DataView(ds.Tables["Plane"], "airline_id = " + dg.CurrentRow.Cells[0].Value, "", DataViewRowState.CurrentRows); 
      dg2.DataSource = dv; 
     } 

     private void display_btn_Click(object sender, EventArgs e) 
     { 
      dg.DataSource = ds.Tables[0]; 
     } 

     private void dg2_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      DataGridViewRow row = dg2.Rows[e.RowIndex]; 
      aidbox.Text = row.Cells["airline_id"].Value.ToString(); 
      pid_box.Text = row.Cells["plane_id"].Value.ToString(); 
      name_box.Text = row.Cells["name"].Value.ToString(); 
      model_box.Text = row.Cells["model"].Value.ToString(); 
      fc_box.Text = row.Cells["f_seats"].Value.ToString(); 
      sc_box.Text = row.Cells["s_seats"].Value.ToString(); 
      bs_box.Text = row.Cells["b_seats"].Value.ToString(); 
      weight_box.Text = row.Cells["p_weight"].Value.ToString(); 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      DataRow[] row_update = ds.Tables["Plane"].Select("airline_id = " + aidbox.Text); 
      try 
      { 
       var row = row_update[0]; 
       row["airline_id"] = int.Parse(aidbox.Text); 
       row["plane_id"] = int.Parse(pid_box.Text); 
       row["name"] = name_box.Text; 
       row["model"] = model_box.Text; 
       row["f_seats"] = int.Parse(fc_box.Text); 
       row["s_seats"] = int.Parse(sc_box.Text); 
       row["b_seats"] = int.Parse(bs_box.Text); 
       row["p_weight"] = float.Parse(weight_box.Text); 
      } 
      catch (Exception ex) { MessageBox.Show(ex.Message); } 

      try 
       { 
        builder = new SqlCommandBuilder(data_adapter); 
        data_adapter.UpdateCommand = builder.GetUpdateCommand(); 

        data_adapter.Update(ds, "Plane"); 

       } 
       catch (SqlException ex) { MessageBox.Show(ex.Message); } 
      } 

     } 

답변

0

data_adapter 개체를 올바르게 초기화하지 않았습니다. 이 라인에서

당신은 당신이 그것에서 선택 명령을 지정할 필요가 데이터 어댑터

public SqlDataAdapter data_adapter = new SqlDataAdapter(); 

의 객체를 만들었습니다.

public SqlDataAdapter data_adapter = new SqlDataAdapter(); 
data_adapter.SelectCommand = new SqlCommand("Select Query", connection); 

또는 신고시 지정하십시오.

public SqlDataAdapter data_adapter = new SqlDataAdapter("Select Query", connection); 

내가 "(평면이 = 새로운 SqlDataAdapter를 공개하여 SqlDataAdapter data_adapter"와 같은 쓴

//Create this object at class level 
public partial class AirlineReservation 
{ 
    SqlDataAdapter childDataAdapter; 
... 
... 

private void getData() 
{ 
    use it like this in getData() function 
    childDataAdapter = new SqlDataAdapter("select * from Plane", connection); 
    ... 
    ... 


private void button2_Click(object sender, EventArgs e) 
{ 
    DataRow[] row_update = ds.Tables["Plane"].Select("airline_id = " + aidbox.Text); 
    try 
    { 
     var row = row_update[0]; 
     row["airline_id"] = int.Parse(aidbox.Text); 
     row["plane_id"] = int.Parse(pid_box.Text); 
     row["name"] = name_box.Text; 
     row["model"] = model_box.Text; 
     row["f_seats"] = int.Parse(fc_box.Text); 
     row["s_seats"] = int.Parse(sc_box.Text); 
     row["b_seats"] = int.Parse(bs_box.Text); 
     row["p_weight"] = float.Parse(weight_box.Text); 
    } 
    catch (Exception ex) { MessageBox.Show(ex.Message); } 

    try 
     { 
      builder = new SqlCommandBuilder(childDataAdapter); 
        //This line is not required 
      //data_adapter.UpdateCommand = builder.GetUpdateCommand(); 
        ds.EndInit(); 
      childDataAdapter.Update(ds, "Plane"); 

     } 
     catch (SqlException ex) { MessageBox.Show(ex.Message); } 
    } 

} 
+0

하기 권장 코드 ", ConnectionState);" Plane은 내가 업데이트하고 싶은 테이블입니다. 그러나 다음과 같은 오류가 발생합니다. 오류 인수 2 : 'System.Data.ConnectionState'에서 'string'으로 변환 할 수 없거나 오류 인수 2 : 'System.Data.ConnectionState'에서 'System'으로 변환 할 수 없습니다. Data.SqlClient.SqlConnection ' – user3421241

+0

ConnectionState를 연결로 변경하십시오. – Shell

+0

'data_adapter' 객체가 생성 된 클래스 수준에서'childDataAdapter'를 선언하고 data_adapter 대신이 클래스를 사용하십시오. – Shell

관련 문제