ado.net中缺少主键异常 [英] missing primary key exception in ado.net
本文介绍了ado.net中缺少主键异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但是先生!
我已经更改了代码,但现在显示索引超出范围异常.
这是我的代码:
but sir!
i have changed my code but now it''s showing index out of range exception.
here''s my code:
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 Data_entry
{
public partial class Form1 : Form
{
DataTable dtable;
DataRow dr;
int code;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tkDataSet1.tushar1' table. You can move, or remove it, as needed.
this.tushar1TableAdapter.Fill(this.tkDataSet1.tushar1);
txtSr.Enabled = false;
txtName.Enabled = false;
txtContact.Enabled = false;
cmdSave.Enabled = false;
}
private void cmdAdd_Click(object sender, EventArgs e)
{
cmdSave.Enabled = true;
txtSr.Enabled = true;
txtName.Enabled = true;
txtContact.Enabled = true;
txtSr.Text = " ";
txtName.Text = " ";
txtContact.Text = " ";
int ctr, len;
string codeval;
dtable = tkDataSet1.Tables["tushar1"];
len = dtable.Rows.Count - 1;
dr = dtable.Rows[len];
code = Convert.ToInt32(dr["code"]);
codeval = code.ToString();
ctr = Convert.ToInt32(codeval);
if (ctr >= 20)
{
ctr = ctr + 1;
txtSr.Text = " " + ctr;
}
else if(ctr >= 34)
{
cmdAdd.Enabled = false;
}
}
private void cmdSave_Click(object sender, EventArgs e)
{
dtable = tkDataSet1.Tables["tushar1"];
dr = dtable.NewRow();
dr[0] = txtSr.Text;
dr[1] = txtName.Text;
dr[2] = txtContact.Text;
dtable.Rows.Add(dr);
tushar1TableAdapter.Update(tkDataSet1);
txtSr.Text = Convert.ToString(dr[0]);
txtSr.Enabled = false;
txtName.Enabled = false;
txtContact.Enabled = false;
this.tushar1TableAdapter.Fill(this.tkDataSet1.tushar1);
cmdAdd.Enabled = true;
cmdSave.Enabled = false;
}
private void cmdDel_Click(object sender, EventArgs e)
{
int code;
code = Convert.ToInt32(txtSr.Text);
dr = tkDataSet1.Tables["tushar1"].Rows.Find(code);
dr.Delete();
tushar1TableAdapter.Update(tkDataSet1);
}
}
}
推荐答案
解决方案...添加主键.
DataTable.PrimaryKey [
What is the solution...add a primary key.
DataTable.PrimaryKey[^]
这篇关于ado.net中缺少主键异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文