C# 尝试更新SQL Server数据库中的值
目前,我在更新SQL Server数据库中的值时遇到问题 我正在做一个项目,该项目将学生的姓名、出生日期和性别存储在数据库中,并且可以通过windows窗体进行编辑和更新。到目前为止,浏览数据工作正常,但是当表单上的值发生更改并单击“更新”按钮时,程序会崩溃,并出现错误 未处理NullReferenceException。对象引用未设置为对象的实例 错误,我目前不知道是什么导致了它 目前,我有一个DBConnector类,它打开到数据库和数据集的连接,并将其传递到windows窗体,该窗体包含显示值的文本框和浏览数据的导航按钮。下面是这个类和windows窗体的代码,我希望它能比我解释的更好 DBConnector.cs类 studentdetails.cs Windows窗体C# 尝试更新SQL Server数据库中的值,c#,sql,sql-server,C#,Sql,Sql Server,目前,我在更新SQL Server数据库中的值时遇到问题 我正在做一个项目,该项目将学生的姓名、出生日期和性别存储在数据库中,并且可以通过windows窗体进行编辑和更新。到目前为止,浏览数据工作正常,但是当表单上的值发生更改并单击“更新”按钮时,程序会崩溃,并出现错误 未处理NullReferenceException。对象引用未设置为对象的实例 错误,我目前不知道是什么导致了它 目前,我有一个DBConnector类,它打开到数据库和数据集的连接,并将其传递到windows窗体,该窗体包含显
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;
namespace StudentRecords
{
public partial class studentdetails : Form
{
DataSet StudentDataSet;
//set variables for limit
int Limit = 0;
int current = 0;
public studentdetails()
{
InitializeComponent();
}
public studentdetails(DataSet ds)
: this()
{
StudentDataSet = ds;
//set the limit of records we can navigate
Limit = StudentDataSet.Tables["Students"].Rows.Count;
NavigateRecords();
}
public studentdetails(DBConnector db) : this()
{ }
public studentdetails(DBConnector db, int sRow) : this(db)
{
current = sRow;
NavigateRecords();
//turn on editing
plEdit.Visible = true;
//set our local dataset object to point to the passed in one
DBConnection = db;
StudentDataSet = db.DBDataSet;
Limit = StudentDataSet.Tables["Students"].Rows.Count;
NavigateRecords();
}
//navigate records function to move through the records
public void NavigateRecords()
{ //create a datarow object and set it to be the first row in the dataset
DataRow dRow = StudentDataSet.Tables["Students"].Rows[current];
//set the form text to add the current record number
this.Text += " for record " + dRow.ItemArray.GetValue(0).ToString();
//fill the text boxes with the database values
txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
txtMiddleName.Text = dRow.ItemArray.GetValue(2).ToString();
txtLastName.Text = dRow.ItemArray.GetValue(3).ToString();
txtDOB.Text = dRow.ItemArray.GetValue(4).ToString();
txtgender.Text = dRow.ItemArray.GetValue(5).ToString();
}
//update the label for the dtatbase
private void UpdateCount()
{
txtCount.Text = (current + 1).ToString() + " of " + Limit.ToString();
}
private void btn_next_Click(object sender, EventArgs e)
{
if (current != Limit - 1)
{
current++;
NavigateRecords();
}
else
{
MessageBox.Show("Last Record", "Information", 0, MessageBoxIcon.Information);
}
}
private void btn_prev_Click(object sender, EventArgs e)
{
if (current > 0)
{
current--;
NavigateRecords();
}
else
{
MessageBox.Show("First Record", "Information", 0, MessageBoxIcon.Information);
}
}
private void btn_Last_Click(object sender, EventArgs e)
{
if (current != Limit - 1)
{
current = Limit - 1;
NavigateRecords();
}
}
private void btn_first_Click(object sender, EventArgs e)
{
if (current != 0)
{
current = 0;
NavigateRecords();
}
}
public DBConnector DBConnection { get; set; }
private void btn_save_Click(object sender, EventArgs e)
{
{
//create a new datarow
DataRow dRow = StudentDataSet.Tables["Students"].NewRow();
//set the data to be the values from the text boxes
dRow[1] = txtFirstName.Text;
dRow[2] = txtMiddleName.Text;
dRow[3] = txtLastName.Text;
dRow[4] = txtDOB.Text;
dRow[5] = txtgender.Text;
//add the row to our dataset
StudentDataSet.Tables["Studentstbl"].Rows.Add(dRow);
//increase the limit as we have a new record
Limit++;
//move to the newly entered Student
current = Limit - 1;
DBConnection.UpdateDB(StudentDataSet, "Students");
MessageBox.Show("Record Saved", "Information", 0, MessageBoxIcon.Information);
NavigateRecords();
}
}
private void btn_update_Click(object sender, EventArgs e)
{
{ //get the current row
DataRow dRow = StudentDataSet.Tables["Students"].Rows[current];
//set the dataset values to those in the textboxes
dRow[1] = txtFirstName.Text;
dRow[2] = txtMiddleName.Text;
dRow[3] = txtLastName.Text;
dRow[4] = txtDOB.Text;
dRow[5] = txtgender.Text;
//call the update method in our DB connector class
DBConnection.UpdateDB(StudentDataSet, "Students");
//display a message box
MessageBox.Show("Record updated", "Information", 0, MessageBoxIcon.Information);
NavigateRecords();
}
}
}
}
数据库名为Students.mdf,表名为Studentstbl,第行抛出错误:
DBConnection.UpdateDB(StudentDataSet, "Students");
我知道这可能是我遗漏了一些愚蠢的东西,但我对C代码还相当陌生,所以如果是这样的话,请原谅我:
编辑:我还应该指出,studentdetails是一个次要表单,主表单只包含一个按钮,可以在其中打开该表单,它包含以下代码:
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;
namespace Coursework3
{
public partial class Form1 : Form
{
DBConnector Students;
public Form1()
{
InitializeComponent();
//intansiate the object in memory
Students = new DBConnector();
}
private void btnstudentdetails_Click(object sender, EventArgs e)
{
new studentdetails(Students.DBDataSet).ShowDialog();
}
}
}
如果您是新手,请学习使用调试器。。。我敢打赌,如果您能够跟踪特定代码行的异常,您将自己发现问题;同意,在调试模式下运行此命令,您应该正好在导致null引用异常的代码行结束。这是一个未使用值初始化的对象,但正在尝试以某种方式访问它-调用函数、属性等。如果仍有问题,请在post中留下注释,以防止通过调试跟踪它。您的DBConnection为null,几乎可以肯定,因为它只在三个构造函数中的一个中设置。我猜这不是您要调用的。NullReferenceException是.NET堆栈中最常见和最容易修复的异常之一。我的问题是,DBConnection.UpdateDBStudentDataSet,学生;如果在此行抛出异常,那么哪个对象为null?这里的断点可能会告诉您。。。
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;
namespace Coursework3
{
public partial class Form1 : Form
{
DBConnector Students;
public Form1()
{
InitializeComponent();
//intansiate the object in memory
Students = new DBConnector();
}
private void btnstudentdetails_Click(object sender, EventArgs e)
{
new studentdetails(Students.DBDataSet).ShowDialog();
}
}
}