2017-06-13 5 views
0

新しいレコードを保存した後、私のデータベースのデータをdataGridViewに表示したい。ボタンをクリックすると、データは保存されますが、データグリッドビューには表示されません。そのデータをどのように表示できますか?新しいレコードを追加した後にデータグリッドにデータを表示

private void btn_add_Click(object sender, EventArgs e) 
    { 

     { 

      if (textBox_tarikh.Text == "" || textBox_resit.Text == "" || textBox_bayaran.Text == "") 
      { 
       MessageBox.Show("Please Fill In The Blank"); 

      } 
      else 
      { 

        String bResult = textBox_ic.Text; 
        string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\acap\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30"; // add your conncetion string here 
        SqlConnection connection = new SqlConnection(connectionString); 
        SqlCommand cmd = new SqlCommand("INSERT Pembayaran (Description, Date, No_Resit, Payment, Studentic) VALUES (@Description, @date, @resit, @payment, @val)", connection); 
        cmd.Parameters.AddWithValue("@val", bResult); 
        cmd.Parameters.AddWithValue("@Description", label4.Text); 
        cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(textBox_tarikh.Text)); 
        cmd.Parameters.AddWithValue("@resit", textBox_resit.Text); 
        cmd.Parameters.AddWithValue("@payment", textBox_bayaran.Text); 
        SqlDataAdapter dataadapter = new SqlDataAdapter(cmd); 
        DataSet ds = new DataSet(); 
        connection.Open(); 
        dataadapter.Fill(ds, "pembayaran_table"); 
        connection.Close(); 
        dataGridView3.DataSource = ds; 
        dataGridView3.DataMember = "pembayaran_table"; 

        cmd.Connection.Open(); 
        try 
        { 
         cmd.ExecuteNonQuery(); 
         MessageBox.Show("Data saved Successfully"); 
       } 
        catch (Exception ex) 
        { 
         //throw new Exception("Error " + ex.Message); 
         MessageBox.Show("Receipt No. is already use"); 
        } 

      } 
+0

グリッドをバインドするためのコードがあるの? 'cmd.ExecuteNonQuery();' –

答えて

0

あなたは試すことができます:

private void btn_add_Click(object sender, EventArgs e) 
{ 
    string bResult = textBox_ic.Text; 
    if (textBox_tarikh.Text == "" || textBox_resit.Text == ""||textBox_bayaran.Text == "") 
     { 
      MessageBox.Show("Please Fill In The Blank"); 
     } 
    string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\acap\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30"; 
    SqlConnection connection = new SqlConnection(connectionString); 
    SqlCommand cmd ; 
    try 
    { 
     if(connection.State == ConnectionState.Closed) connection.Open(); 
     cmd = new SqlCommand("INSERT Pembayaran (Description, Date, No_Resit, Payment, Studentic) VALUES (@Description, @date, @resit, @payment, @val)", connection); 
     cmd.Parameters.AddWithValue("@val", bResult); 
     cmd.Parameters.AddWithValue("@Description", label4.Text); 
     cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(textBox_tarikh.Text)); 
     cmd.Parameters.AddWithValue("@resit", textBox_resit.Text); 
     cmd.Parameters.AddWithValue("@payment", textBox_bayaran.Text); 
     cmd.Executenonquery(); 

     SqlDataAdapter da = new SqlDataAdapter("Select * from Pembayaran",connection); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     dataGridView3.DataSource = dt; 
    } 
    catch(Exception ex) 
    { 
    } 
    finally 
    { 
     if(connection.State == ConnectionState.Open) connection.Close(); 
    } 
} 
+0

大丈夫@DuchAnhNguyenありがとうございます。コードは仕事です。 –

0

まず、私はあなたのスニペット内のコードを結合する任意のデータが表示されないように、あなたがのIDを取得するために@@IDENTITYを利用して、データを挿入した後、それをやろうとすることができる何DataGridView.DataBind();

DataGridView.DataSource = " "などあなたが挿入したものその後、取り出されたidを使って新しいレコードを取得します。

@@IDENTITY

私はあなたが同じクエリを使用し、あなたのコードスニペットに気づきました。

SqlDataAdapter dataadapter = new SqlDataAdapter(cmd); 

あなたは別のSqlStatementを書いて、例えばSqlDataAdapter

からそれを呼び出す必要があります。

// Assumes that connection is a valid SqlConnection object. 
string queryString = 
    "SELECT CustomerID, CompanyName FROM dbo.Customers"; 
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection); 

DataSet customers = new DataSet(); 
adapter.Fill(customers, "Customers"); 

Populating a DataSet from a DataAdapter

+0

の後にこれを呼び出すと、私は上のようなコードを使用して編集しましたが、エラーメッセージ "pembayaran_tableフィールドの子リストは作成できません"と表示されます。 –

+0

@ashrafibrahim私の更新された答えを確認 – Alvin

0

あなただけDBにレコードを挿入した後、あなたのDataGridViewのデータソースを更新する必要があります。

+0

あなたはコードを書くことができます@ビッキーS。 –

+0

@DucAnhNguyen答えはあなたがそれを試してみてください働くことができます –

関連する問題