为何记录不动 [英] Why records not moving
本文介绍了为何记录不动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中的两条记录。当我点击下一步按钮然后发出一条错误消息
位置1没有行
i我正在使用下面的代码。
任何人请帮助我在代码中出错的地方。
Two records in my database.when i click on next button then one error message coming
"There is no row at position 1"
i am using below code .
anyone please help me where i am doing mistake in 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;
namespace MyProject
{
public partial class Form1 : Form
{
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);
DataTable tbl = new DataTable();
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 button8_Click(object sender, EventArgs e)
{
DataTable tbl = new DataTable();
if (i > tbl.Rows.Count - 1)
{
i++;
display(tbl);
}
else
{
MessageBox.Show("this is last records");
}
}
推荐答案
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;
namespace MyProject
{
public partial class Form1 : Form
{
int i = 0;
DataTable tbl = new DataTable();
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);
//DataTable tbl = new DataTable();
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 button8_Click(object sender, EventArgs e)
{
//DataTable tbl = new DataTable();
if (i <=tbl.Rows.Count)
{
i++;
display(tbl);
}
else
{
MessageBox.Show("this is last records");
}
}
继Jawad Ahmed Tanoli的解决方案...
问题的原因是可变范围 [ ^ ]
DataTabletbl
与Button8_Click事件中的tbl
不同!
Jawad的代码所做的是将DataTable tbl的声明移动到 Form 级别范围,因此同一个实例(即在Form_Load中加载的数据)可用于此Form类中的所有函数。
Further to Jawad Ahmed Tanoli's solution...
The cause of your problem is Variable Scope[^]
The DataTabletbl
is not the same as thetbl
in your Button8_Click event!
What Jawad's code does is move the declaration of DataTable tbl to Form level scope, so the same instance (i.e. the data that is loaded at Form_Load) is available to all the functions within this Form class.
private void button8_Click(object sender, EventArgs e)
{
DataTable tbl = new DataTable();
//this datatable tbl is not filled here with data
if (i > tbl.Rows.Count - 1)
{
i++;
display(tbl);
}
else
{
MessageBox.Show("this is last records");
}
}
这篇关于为何记录不动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文