ibrahim serdar
ibrahim serdar

Reputation: 11

I need help updating my form to show new database records

Basically, I have a WinForm in Visual Studio, that can add, delete, and view records from a database.

What I need to do is after adding, deleting, or editing a record, it needs to refresh the databindings or the database so it pulls accurate data from the database.

I've already tried Refresh() and Update() and tried closing and showing the form again, which it doesn't do.

This is the code I use for adding a record into the database.

private void kaykay_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = 
"insert into RM_DATA 
(`Protokol No`,
 `Küpe No`,
 `Cinsi`,
 `Türü`,
 `Cinsiyeti`,
 `Alındığı Yer`,
 `Ekip`, 
`Alınma Tarihi`,
 `Taburcu Tarihi`,
 `İlgilisi `,
 `Telefon`,
 `Açıklama`,
 `Mikro Çip`,
 `Resim`
) values(
'" + protokolno.Text + "',
'" + kupeno.Text + "',
'" + turu.Text + "',
'" + cinsi.Text + "',
'" + cinsiyeti.Text + "',
'" + alyer.Text + "',
'" + alekip.Text + "',
'" + dateTimePicker1.Text + "',
'" + dateTimePicker2.Text + "',
'" + ilgilisi.Text + "',
'" + ilgilisitelno.Text + "',
'" + aciklama.Text + "',
'" + mikrocip.Text + "',
'" + textBox1.Text + "')";
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Kayit Basariyla Girildi");


        }

What I need it to do is to update the form after the messagebox shows up. It doesn't give me any errors.

EDIT: I should clarify, I am not using datagridview.

Upvotes: 0

Views: 79

Answers (2)

matin azizi
matin azizi

Reputation: 13

You can use Timer_tick to update your database basically you can call show data base function in timer-tick so it will show again your database every x seconds and it will be updated

Upvotes: 0

Mojtaba Nava
Mojtaba Nava

Reputation: 878

This method for Insert, update and delete DisplayData() :

 private void DisplayData()  
    {  
        con.Open();  
        DataTable dt=new DataTable();  
        SqlDataAdapter adapt=new SqlDataAdapter("select * from tbl_Record",con);  
        adapt.Fill(dt);  
        dataGridView1.DataSource = dt;  
        con.Close();  
    }  

private void kaykay_Click(object sender, EventArgs e)
    {
        con.Open();
        OleDbCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = 
        "insert into RM_DATA 
       (`Protokol No`,
       `Küpe No`,
       `Cinsi`,
         ...
       '" + textBox1.Text + "')";
        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Kayit Basariyla Girildi");
        DisplayData();

    }

Upvotes: 0

Related Questions