目前我在更新SQL Server数据库中的值时遇到问题。
我正在开展一个项目,将学生姓名,出生日期和性别存储在数据库中,并且可以通过Windows窗体进行编辑和更新。到目前为止浏览数据工作正常,但是当在表单上更改值并单击更新按钮时,程序崩溃
NullReferenceException未处理。对象引用未设置为对象的实例。
错误,我目前对导致它的原因感到茫然。
目前我有一个DBConnector
类,它打开与数据库和数据集的连接,并将其传递给包含文本框的窗体,以显示值和浏览数据的导航按钮。 (下面是类和windows窗体的代码,我希望能解释比我更好的东西)。
DBConnector.cs
班
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace StudentRecords
{
public class DBConnector
{
//Properties
//connection object
private System.Data.SqlClient.SqlConnection Conn;
//dataset object
private StudentsDataSet Ds1;
//dataadapter object
private System.Data.SqlClient.SqlDataAdapter Da;
//constructors
public DBConnector()
{
//call initialisation
init();
}
//Methods
//initialisation method
public void init()
{
//create the memory space for the connection object
Conn = new System.Data.SqlClient.SqlConnection();
//create the memory space for the dataset
Ds1 = new StudentsDataSet();
//set the connection string to the location of our database file
Conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Students.mdf;Integrated Security=True;User Instance=True";
//open the conenction
Conn.Open();
//create a query to get all the records from the table
string query = "SELECT * from Studentstbl";
//create the data adapter for the database
Da = new System.Data.SqlClient.SqlDataAdapter(query, Conn);
//use it to fill the dataset as the first parameter the second is a name for the table we use later on
Da.Fill(Ds1, "Students");
//close the connection
Conn.Close();
System.Windows.Forms.MessageBox.Show("Database connection Open", "Success");
}
public StudentsDataSet DBDataSet
{
get
{
return Ds1;
}
}
//update method
public void UpdateDB(DataSet ds, string table)
{
//create a command builder to reconnect to the database
System.Data.SqlClient.SqlCommandBuilder cb;
//set the comamnd builder to be our existing dataadapter
cb = new System.Data.SqlClient.SqlCommandBuilder(Da);
Da.Update(ds, table);
}
}
}
studentdetails.cs
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();
}
}
}