新人求助 下面是代码和错误提示图片
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 通讯录窗体
{
public partial class Form主界面 : Form
{
public Form主界面()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form添加信息 from = new Form添加信息();
from.Show();
}
private void Form主界面_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void Form主界面_Load(object sender, EventArgs e)
{
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
}
private void btnUpd_Click_1(object sender, EventArgs e)
{
try
{
if (btnUpd.Text == "修改记录")
{
dgvAddress.ReadOnly = false;
btnUpd.Text = "保存修改";
btnAdd.Enabled = false;
btnDel.Enabled = false;
}
else
{
sqlDataAdapter1.Update(txtContant, "Address");
btnUpd.Text = "修改记录";
dgvAddress.ReadOnly = true;
btnAdd.Enabled = true;
btnDel.Enabled = true;
}
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message);
}
}
private void btnPwd_Click_1(object sender, EventArgs e)
{
this.Hide();
Form修改密码 form = new Form修改密码();
form.Show();
}
private void btnDel_Click(object sender, EventArgs e)
{
try
{
DialogResult result = MessageBox.Show("真的要删除当前记录吗?", "删除记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
int n = dgvAddress.CurrentRow.Index;
DataTable myTab = new DataTable();
myTab = txtContant.Tables["address"];
DataRow myRow = myTab.Rows[n];
string xh = myRow[0].ToString();
int i = int.Parse(xh);
string connectionString = "Data Source=.;database=addressbook;Integrated Security=True";
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = connectionString;
SqlCommand myCom = myCon.CreateCommand();
= "delete address where 序号=" + i + "";
myCon.Open();
myCom.ExecuteNonQuery();
myCom.Dispose();
myCon.Close();
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
MessageBox.Show("删除成功");
}
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message);
}
}
private void btnCancel_Click_1(object sender, EventArgs e)
{
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
btnAdd.Enabled = true;
btnDel.Enabled = true;
btnUpd.Text = "修改记录";
dgvAddress.ReadOnly = true;
}
private void btnExit_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void Form主界面_Load_1(object sender, EventArgs e)
{
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
}
private void btnALL_Click(object sender, EventArgs e)
{
dgvAddress.DataSource = txtContant;
dgvAddress.DataMember = "address";
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
}
private void btnselect_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=.;database=addressbook;Integrated Security=True";
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = connectionString;
SqlCommand myCom = myCon.CreateCommand();
SqlDataAdapter myAdpter = new SqlDataAdapter();
DataSet mySet = new DataSet();
int n;
switch (cbxClass.SelectedIndex)
{
case 0: = "select * from address where 姓名='" + txtContant.Text + "'";
myAdpter.SelectCommand = myCom;
n = myAdpter.Fill(mySet, "address");
dgvAddress.DataSource = mySet;
dgvAddress.DataMember = "address";
if (n == 0)
{
MessageBox.Show("未找到相应信息", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
}
break;
case 1: = "select * from address where 电话='" + txtContant.Text + "'";
myAdpter.SelectCommand = myCom;
n = myAdpter.Fill(mySet, "address");
dgvAddress.DataSource = mySet;
dgvAddress.DataMember = "address";
if (n == 0)
{
MessageBox.Show("未找到相应信息", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
}
break;
case 2: = "select * from address where QQ号='" + txtContant.Text + "'";
myAdpter.SelectCommand = myCom;
n = myAdpter.Fill(mySet, "address");
dgvAddress.DataSource = mySet;
dgvAddress.DataMember = "address";
if (n == 0)
{
MessageBox.Show("未找到相应信息", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtContant.Clear();
sqlDataAdapter1.Fill(txtContant, "address");
}
break;
}
}
private void cbxClass_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxClass.SelectedIndex == -1)
{
txtContant.ReadOnly = true;
}
else
{
txtContant.ReadOnly = false;
}
}
}