记录不会添加到我的表中 [英] Records are not added to my table

查看:69
本文介绍了记录不会添加到我的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图运行以下代码时写的代码:

http://www.homeandlearn.co.uk/csharp/csharp_s12p10.html

我发现记录添加到屏幕显示不会添加到表格。

这是代码:





 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间 _46_creating_databse_01_sql_express_02
{
public partial class Form1:Form
{
public Form1(){InitializeComponent(); }


DatabaseConnection objConnect;
string conString;
DataSet ds;
DataRow dRow;
int MaxRows;
int inc = 0 ;

private void Form1_Load_1(对象发​​件人,EventArgs e)
{
尝试
{
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.employeesConnectionString;

objConnect.connection_string = conString;
objConnect.Sql = Properties.Settings.Default.sql;

ds = objConnect.GetConnection;
MaxRows = ds.Tables [ 0 ]。Rows.Count;

NavigateRecords();

}
catch (异常错误)
{
MessageBox.Show(err.Message);
}

}
私有 void NavigateRecords( )
{
dRow = ds.Tables [ 0 ]。行[inc];
txtFirstName.Text = dRow.ItemArray.GetValue( 1 )。ToString();
txtSurname.Text = dRow.ItemArray.GetValue( 2 )。ToString();
txtJobTitle.Text = dRow.ItemArray.GetValue( 3 )。ToString();
txtDepartment.Text = dRow.ItemArray.GetValue( 4 )。ToString();
labelUpdate();
}

private void labelUpdate()
{

label6.Text = 记录 +(inc + 1 )+ of + MaxRows;

}

private void button2_Click(< span class =code-keyword> object sender,EventArgs e)
{
if (inc!= MaxRows - < span class =code-digit> 1 )
{
inc ++;
NavigateRecords();
}
else
{
MessageBox.Show( 没有更多行);
}
}

private void button3_Click( object sender,EventArgs e)
{
if (inc > 0
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show( first record);
}
}

private void button4_Click( object sender,EventArgs e)
{
if (inc!= 0
{
inc = 0 ;
NavigateRecords();
}
}

private void button1_Click( object sender,EventArgs e)
{
if (inc!= MaxRows - 1
{
inc = MaxRows - 1 ;
NavigateRecords();
}
}

私有 void button6_Click( object sender,EventArgs e)
{
txtFirstName.Clear();
txtSurname.Clear();
txtJobTitle.Clear();
txtDepartment.Clear();

btnAddNew.Enabled = false ;
btnSave.Enabled = true ;
btnCancel.Enabled = true ;
}

private void btnCancel_Click( object sender,EventArgs e)
{
NavigateRecords();
btnCancel.Enabled = false ;
btnSave.Enabled = false ;
btnAddNew.Enabled = true ;
}

private void button7_Click( object sender,EventArgs e)
{
DataRow row = ds.Tables [ 0 ]。NewRow( );
row [ 1 ] = txtFirstName.Text;
行[ 2 ] = txtSurname.Text;
row [ 3 ] = txtJobTitle.Text;
row [ 4 ] = txtDepartment.Text;

ds.Tables [ 0 ]。Rows.Add(row);
尝试
{
objConnect.UpdateDatabase(ds);
MaxRows = MaxRows + 1 ;
inc = MaxRows - 1 ;
MessageBox.Show( database updated);
}
catch (异常错误)
{
MessageBox.Show(err.Message);
}
btnCancel.Enabled = false ;
btnSave.Enabled = false ;
btnAddNew.Enabled = true ;



}

private void button9_Click( object sender,EventArgs e)
{
DataRow row = ds.Tables [ 0 ]行[INC]。

row [ 1 ] = txtFirstName.Text;
行[ 2 ] = txtSurname.Text;
row [ 3 ] = txtJobTitle.Text;
row [ 4 ] = txtDepartment.Text;
尝试
{
objConnect.UpdateDatabase(ds);
MessageBox.Show( Record Updated);
}
catch (异常错误)
{
MessageBox.Show(err.Message);
}

}


}
}

解决方案

When I am trying to run this code written in :
http://www.homeandlearn.co.uk/csharp/csharp_s12p10.html
I find that records are added to the screen display are not added to the table .
Here is the code :


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _46_creating_databse_01_sql_express_02
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }
        
            
            DatabaseConnection objConnect;
            string conString;
            DataSet ds;
            DataRow dRow;
            int MaxRows;
            int inc = 0;
        
        private void Form1_Load_1(object sender, EventArgs e)
        {
            try
            {
                objConnect = new DatabaseConnection();
                conString = Properties.Settings.Default.employeesConnectionString;
                
                objConnect.connection_string = conString;
                objConnect.Sql = Properties.Settings.Default.sql;
                
                ds = objConnect.GetConnection;
                MaxRows = ds.Tables[0].Rows.Count;
                
                NavigateRecords();

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }
        private void NavigateRecords()
        {
            dRow = ds.Tables[0].Rows[inc];
            txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
            txtSurname.Text = dRow.ItemArray.GetValue(2).ToString();
            txtJobTitle.Text = dRow.ItemArray.GetValue(3).ToString();
            txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString();
            labelUpdate();
        }

        private void labelUpdate()
        {

            label6.Text = "Record " + (inc + 1) + " of " + MaxRows;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc++;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("No More Rows");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (inc > 0)
            {
                inc--;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("first Record");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (inc != 0)
            {
                inc = 0;
                NavigateRecords();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc = MaxRows - 1;
                NavigateRecords();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            txtFirstName.Clear();
            txtSurname.Clear();
            txtJobTitle.Clear();
            txtDepartment.Clear();

            btnAddNew.Enabled = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            NavigateRecords();
            btnCancel.Enabled = false;
            btnSave.Enabled = false;
            btnAddNew.Enabled = true;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DataRow row = ds.Tables[0].NewRow();
            row[1] = txtFirstName.Text;
            row[2] = txtSurname.Text;
            row[3] = txtJobTitle.Text;
            row[4] = txtDepartment.Text;

            ds.Tables[0].Rows.Add(row);
            try
            {
                objConnect.UpdateDatabase(ds);
                MaxRows = MaxRows + 1;
                inc = MaxRows - 1;
                MessageBox.Show("database updated");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            btnCancel.Enabled = false;
            btnSave.Enabled = false;
            btnAddNew.Enabled = true;



        }

        private void button9_Click(object sender, EventArgs e)
        {
            DataRow row = ds.Tables[0].Rows[inc];
            
            row[1] = txtFirstName.Text;
            row[2] = txtSurname.Text;
            row[3] = txtJobTitle.Text;
            row[4] = txtDepartment.Text;
            try
            {
                objConnect.UpdateDatabase(ds);
                MessageBox.Show("Record Updated");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }


        }
    }

解决方案

这篇关于记录不会添加到我的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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