C#编程中怎样解决未将对象引用设置到对象的实例的问题 DataRow dr = dt.Rows.Find(txtName.Text);出错了
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
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 修改
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet ds = new DataSet();
SqlDataAdapter sda;
DataRow dr;
private void Form1_Load(object sender, EventArgs e)
{
this.contactTableAdapter.Fill(this.通讯录DataSet.Contact);
SqlConnection con = new SqlConnection("server=localHost;database=通讯录;integrated security =true");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Contact_Command ", con);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(ds, "Contact");
dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtName.Text=dataGridView1.SelectedCells[0].Value.ToString();
txtBirthday.Text=dataGridView1.SelectedCells[1].Value.ToString();
txtPhone.Text=dataGridView1.SelectedCells[2].Value.ToString();
txtMail.Text=dataGridView1.SelectedCells[3].Value.ToString();
txtAddress.Text=dataGridView1.SelectedCells[4].Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["Contact"];
SqlDataAdapter sda = new SqlDataAdapter();
// sda.FillSchema(dt,SchemaType.Mapped);
DataRow dr = dt.Rows.Find(txtName.Text);
dr["姓名"] = this.txtName.Text.Trim();
dr["生日"] = this .txtBirthday.Text.Trim();
dr["电话"] = this.txtPhone.Text.Trim();
dr["邮箱"] = this.txtMail.Text.Trim();
dr["地址"] = this.txtAddress.Text.Trim();
SqlCommandBuilder cmdbuilder=new SqlCommandBuilder(sda);
sda.Update(dt);
}
}
} 展开
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 修改
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet ds = new DataSet();
SqlDataAdapter sda;
DataRow dr;
private void Form1_Load(object sender, EventArgs e)
{
this.contactTableAdapter.Fill(this.通讯录DataSet.Contact);
SqlConnection con = new SqlConnection("server=localHost;database=通讯录;integrated security =true");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Contact_Command ", con);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(ds, "Contact");
dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtName.Text=dataGridView1.SelectedCells[0].Value.ToString();
txtBirthday.Text=dataGridView1.SelectedCells[1].Value.ToString();
txtPhone.Text=dataGridView1.SelectedCells[2].Value.ToString();
txtMail.Text=dataGridView1.SelectedCells[3].Value.ToString();
txtAddress.Text=dataGridView1.SelectedCells[4].Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["Contact"];
SqlDataAdapter sda = new SqlDataAdapter();
// sda.FillSchema(dt,SchemaType.Mapped);
DataRow dr = dt.Rows.Find(txtName.Text);
dr["姓名"] = this.txtName.Text.Trim();
dr["生日"] = this .txtBirthday.Text.Trim();
dr["电话"] = this.txtPhone.Text.Trim();
dr["邮箱"] = this.txtMail.Text.Trim();
dr["地址"] = this.txtAddress.Text.Trim();
SqlCommandBuilder cmdbuilder=new SqlCommandBuilder(sda);
sda.Update(dt);
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询