数据库中没有记录但是表单已加载,然后记录显示在表单中 [英] No records in database but form loaded then records show in form

查看:112
本文介绍了数据库中没有记录但是表单已加载,然后记录显示在表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经删除了数据库中的所有记录,现在我的数据库中没有记录。

但是当表单加载时,记录显示在我的表格中。

任何人都请帮忙。为此问题来了。

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.

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);
         }

推荐答案

如果您的数据库是VS项目的一部分,那么您可能已经更新了另一个副本到在IDE中运行项目时加载一个... 参考 [ ^ ]
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[^]


这篇关于数据库中没有记录但是表单已加载,然后记录显示在表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆