问题描述
我在线尝试本教程,添加删除搜索和导航数据库。我已成功建立连接。我可以从Form_load调用NavitegateRecords但是btnNext或btnLast没有发生这种情况。我得到一个NullReferenceException bcoz dRow有空值。另外,只是为了检查是否传递了正确的值我使用了一个messageBox来检查objConnect.Sql的内容......但这是一个错误,说属性不能在这个上下文中使用,因为它缺少get访问器(所以我没有了解如何解决它暂时评论它)。我的代码如下:
I am trying this tutorial from online to add delete search and navigate a database. I have successfully made the connection. I could call the NavitegateRecords from Form_load But btnNext or btnLast such is not happening. Im getting a NullReferenceException bcoz the dRow has null value. Also just to check if correct value is passed I used a messageBox to check the content of objConnect.Sql...But this is giving an error saying "property cannot be used in this context because it lacks the get accessor"(So I not understanding how to solve it commented it out for the time being). My code is as follows:
DatabaseConnection objConnect;
string conString;
DataSet ds;
DataRow dRow;
int MaxRows;
int inc = 0;
SqlConnection cnn = new SqlConnection();
SqlDataAdapter daAdapter = new SqlDataAdapter();
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds=new DataSet();
String SQ;
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.EmployeesConnectionString;
//MessageBox.Show(conString);
objConnect.connection_string = conString;
SQ=Properties.Settings.Default.SQL;
objConnect.Sql = SQ;
MessageBox.Show(objConnect.Sql);
ds = objConnect.GetConnection;
MaxRows = ds.Tables[0].Rows.Count;
MessageBox.Show(MaxRows.ToString());
try
{
NavigateRecords(ds);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void NavigateRecords(DataSet ds)
{
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();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != (MaxRows-1))
{
inc= inc+1;
NavigateRecords(ds);
}
else
{
if (inc != (MaxRows - 1))
{
inc = (MaxRows-1);
NavigateRecords(ds);
}
MessageBox.Show("No More Rows");
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc= inc-1;
NavigateRecords(ds);
}
else
{
if (inc != 0)
{
inc = 0;
NavigateRecords(ds);
}
MessageBox.Show("First Record");
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc = MaxRows - 1;
NavigateRecords(ds);
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc = 0;
NavigateRecords(ds);
}
}
推荐答案
这篇关于看不到下一个上一个或上一个和第一个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!