Click here to Skip to main content
15,885,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have deleted all records from database now no records in my database.
but when form loaded then records show in my form.
anyone please help.why this problem coming.
C#
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)
        {

            RefreshData();
        }
           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!");
            
               }
       
        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);
         }
Posted
Updated 22-Mar-14 4:22am
v2
Comments
Krunal Rohit 22-Mar-14 10:22am    
Have you checked ??

-KR
manish7664 22-Mar-14 14:20pm    
i chked no records in database . if i remove refreshdata() from Form load then nothing show in form textbox.

CHill60 22-Mar-14 10:34am    
As an aside, you should be using parameterized queries to reduce the risk of sql injection. See this tutorial http://www.dotnetperls.com/sqlclient[^]
manish7664 22-Mar-14 14:24pm    
if i remove refreshdata() from from load then nothing show in form textbox.
now could you please tell me what's the problem.
CHill60 22-Mar-14 14:36pm    
Well nothing will show as it is the RefreshData() function that is populating the text box. Did you check the link in the solution I posted?

1 solution

If you have the database as part of your VS project then you may have updated a different copy to the one loaded when running the project in the IDE ...reference[^]
 
Share this answer
 

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