我正在编写我的第一个c#应用程序,它必须连接到数据库。我想将数据从TextBoxes保存到数据库,但我在标记行中有问题。
连接数据库的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace prviTelefonskiImenik
{
class DatabaseConnection
{
private string sql_string;
private string strCon;
System.Data.SqlClient.SqlDataAdapter da_1;
public string Sql
{
set { sql_string = value; }
}
public string connection_string
{
set { strCon = value; }
}
public System.Data.DataSet GetConnection
{
get
{ return MyDataSet(); }
}
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_Data_1");
//da_1.Fill(dat_set, "kontakti");
con.Close();
return dat_set;
}
public void UpdateDatabase(System.Data.DataSet ds)
{
System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
cb.DataAdapter.Update(ds.Tables[0]);
}
}
}
用于将数据从TextBox保存到数据库的代码:
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 prviTelefonskiImenik
{
public partial class dodajKontakt : Form
{
public dodajKontakt()
{
InitializeComponent();
}
DatabaseConnection objConnect;
string conString;
DataSet ds;
DataRow dRow;
int MaxRows;
int inc = 0;
private void dodajKontakt_Load(object sender, EventArgs e)
{
try
{
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.kontaktiConnectionString;
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];
txtIme.Text = dRow.ItemArray.GetValue(1).ToString();
txtPriimek.Text = dRow.ItemArray.GetValue(2).ToString();
txtEmso.Text = dRow.ItemArray.GetValue(3).ToString();
txtTelefon.Text = dRow.ItemArray.GetValue(4).ToString();
txtEmail.Text = dRow.ItemArray.GetValue(5).ToString();
}
private void shrani_Click(object sender, EventArgs e)
{
**DataRow row = ds.Tables[0].NewRow();**
row[1] = txtIme.Text;
row[2] = txtPriimek.Text;
row[3] = txtEmso.Text;
row[4] = txtTelefon.Text;
row[5] = txtEmail.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);
}
}
}
}
编译时,Visual Studio 2013在此处发现错误: DataRow row = ds.Tables [0] .NewRow(); 附加信息:对象引用未设置为对象的实例。
你能帮我解决一个问题吗?谢谢!
答案 0 :(得分:0)
当此代码调用时,检查ds.Tables是否为空
答案 1 :(得分:0)
您正面临这个问题,因为您的数据集没有表格对象,并且您正在对其进行更改,这就是为什么您会收到对象引用的错误
在对其进行更改之前检查计数
if(ds!= null && ds.Tables.Count> 0)
{
DataRow row = ds.Tables[0].NewRow();
}