读书人

对绑定数据进行修改时出现有关问题!

发布时间: 2012-12-24 10:43:14 作者: rapoo

对绑定数据进行修改时出现问题!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 学生管理系统
{
public partial class Modify : Form
{
string no;
string cno;
public string No
{
get
{
return no;
}
set
{
no = value;
}
}
public string CNo
{
get
{
return cno;
}
set
{
cno = value;
}
}
SqlDataAdapter adapter = null;
DataSet dataset = new DataSet();
SqlDataAdapter adapter1 = null;
DataSet dataset1 = new DataSet();
SqlDataAdapter adapter2 = null;
DataSet dataset2 = new DataSet();
string connstr = ConfigurationManager.ConnectionStrings["ConnStr1"].ConnectionString;
public Modify()
{
InitializeComponent();
}

private void Modify_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connstr);

conn.Open();
SqlCommand cmd = conn.CreateCommand();



cmd.CommandText = "select * from student where No=@no";
cmd.Parameters.Add(new SqlParameter("no", no));
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dataset = new DataSet();
adapter.Fill(dataset);
textBox1.DataBindings.Add("Text", dataset.Tables[0], "No");
textBox2.DataBindings.Add("Text", dataset.Tables[0], "Name");
textBox3.DataBindings.Add("Text", dataset.Tables[0], "Major");
textBox4.DataBindings.Add("Text", dataset.Tables[0], "Birthday");
textBox5.DataBindings.Add("Text", dataset.Tables[0], "Mark");
textBox6.DataBindings.Add("Text", dataset.Tables[0], "About");
textBox8.DataBindings.Add("Text", dataset.Tables[0], "Sec");

SqlCommand cmd1 = conn.CreateCommand();


cmd1.CommandText = "select * from Relation where No=@No";
cmd1.Parameters.Add(new SqlParameter("No", no));
adapter1 = new SqlDataAdapter(cmd1);
dataset1 = new DataSet();
adapter1.Fill(dataset1);
SqlCommand cmd2 = conn.CreateCommand();


cmd2.CommandText = "select Course.CName as '课程名',Relation.Mark as '成绩',KKXQ as '开课学期',XS as '学时',XF as '学分' from Course,Relation where Course.CNo=Relation.CNo";


adapter2 = new SqlDataAdapter(cmd2);
dataset2 = new DataSet();
adapter2.Fill(dataset2);
dataGridView1.DataSource = dataset2.Tables[0].DefaultView;



}

private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder build = new SqlCommandBuilder(adapter);
SqlCommandBuilder bulid1 = new SqlCommandBuilder(adapter1);
SqlCommandBuilder bulid2 = new SqlCommandBuilder(adapter2);
adapter.Update(dataset.Tables[0]);
adapter1.Update(dataset1.Tables[0]);
adapter2.Update(dataset2.Tables[0]);
}
}
}

但对绑定的数据修改后,按保存按钮式出现如下错误:

[解决办法]
哪一行出错也截上来 这样不知道出错位置不好判断
[解决办法]

引用:
哪一行出错也截上来 这样不知道出错位置不好判断



adapter.Update(dataset.Tables[0]);

读书人网 >C#

热点推荐