Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am having a problem. I am using SQL Server 2008 as my DB. Everything is okay loading, adding, data.

Problem is when i insert a record in database. After inserting a record, it does not show properly in the form unless i close the form and reload it again. Is there any method in C# by which we can refresh the table and can load it in the form same time without restarting the form?

Thanks
//This is My code//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;

namespace MyProject
{

public partial class Form1 : Form
{
DataTable tbl = new DataTable();
int i = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
conn.Open();
SqlCommand command = new SqlCommand("select * from emp_detail", conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
adp.Fill(tbl);
display(tbl);


}
private void display(DataTable tbl)
{
txtempid.Text = tbl.Rows[i][0].ToString();
txtname.Text = tbl.Rows[i][1].ToString();
txtsurname.Text = tbl.Rows[i][2].ToString();
txtfathername.Text = tbl.Rows[i][3].ToString();
dtdob.Text = tbl.Rows[i][4].ToString();
cbgender.Text = tbl.Rows[i][5].ToString();
cbcity.Text = tbl.Rows[i][6].ToString();
txtcontactno.Text = tbl.Rows[i][7].ToString();
dtdoj.Text = tbl.Rows[i][8].ToString();
txtdept.Text = tbl.Rows[i][9].ToString();
txtdesig.Text = tbl.Rows[i][10].ToString();
txtqualification.Text = tbl.Rows[i][11].ToString();
rtaddress.Text = tbl.Rows[i][12].ToString();
rtremarks.Text = tbl.Rows[i][13].ToString();

}
private void button4_Click(object sender, EventArgs e)

{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
conn.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "insert into emp_detail(empid,name,surname,fathername,dob,gender,city,contactno,doj,department,designation,qualification,address,remarks) values('" + txtempid.Text + "','" + txtname.Text + "','" + txtsurname.Text + "','" + txtfathername.Text + "','" + dtdob.Text + "','" + cbgender.Text + "','" + cbcity.Text + "','" + txtcontactno.Text + "','" + dtdoj.Text + "','" + txtdept.Text + "','" + txtdesig.Text + "','" + txtqualification.Text + "','" + rtaddress.Text + "','" + rtremarks.Text + "')";
command.Connection = conn;
command.ExecuteNonQuery();
MessageBox.Show("Saving is done!");
}
Posted

1 solution

After saving you have to refresh your table.

C#
private void Form1_Load(object sender, EventArgs e)
{
    RefreshData();
}

private void RefreshData() 
{
    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    conn.Open();
    SqlCommand command = new SqlCommand("select * from emp_detail", conn);
    SqlDataAdapter adp = new SqlDataAdapter(command);
    adp.Fill(tbl);
    display(tbl);
}

private void button4_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data  Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    conn.Open();
    SqlCommand command = new SqlCommand();
    command.CommandText = "insert into emp_detail(empid,name,surname,fathername,dob,gender,city,contactno,doj,department,designation,qualification,address,remarks) values('" + txtempid.Text + "','" + txtname.Text + "','" + txtsurname.Text + "','" + txtfathername.Text + "','" + dtdob.Text + "','" + cbgender.Text + "','" + cbcity.Text + "','" + txtcontactno.Text + "','" + dtdoj.Text + "','" + txtdept.Text + "','" + txtdesig.Text + "','" + txtqualification.Text + "','" + rtaddress.Text + "','" + rtremarks.Text + "')";
    command.Connection = conn;
    command.ExecuteNonQuery();
    MessageBox.Show("Saving is done!");

    //here you can refresh your table
    RefreshData();
} 
 
Share this answer
 
v2
Comments
manish7664 20-Mar-14 8:06am    
Thank you notnitrial.
norbitrial 20-Mar-14 8:09am    
Your welcome! Can you please set my solution to the answer for your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900