Niranjan Sonachalam
Niranjan Sonachalam

Reputation: 1625

Unable to edit tables in SQL Server 2008 database using C#

I am creating an application in c# which requires me to embed a database, I added a new Service based Database . I added it to the application, I created a table via the designer and added data to it. Here I started facing problems. I am able to add new records but unable to edit any rows. I noted a weird behavior.

enter image description here

enter image description here

I am getting an error

The row values updated or deleted either do not make the row unique or they alter multiple rows

enter image description here

I have neither mentioned a unique constraint nor a not null constraint. All the columns are of data type text. I am not sure what causes this problem. Can somebody explain me whats goin on??

PS: and sometimes,I also get an error as seen in Screenshot 2. For such cases,when I click on Execute SQL , then the data is getting inserted.


***After using an Identity column, the issue is solved. Thanks a lot guys :) 

Now another issue is coming up, I am able to edit in the designer but I also have a dataGridView binded to the database and I am not able to edit(update) values in it, any suggestions?? This is the code, I am using

private void saveButton_Click(object sender, EventArgs e)
        {
                this.Validate();
                this.core3BindingSource.EndEdit();
                this.core3TableAdapter.Update(this.coreDataSet.Core3);
        }***

Upvotes: 0

Views: 689

Answers (2)

Ajay
Ajay

Reputation: 6590

Using this you can avoid duplicate data

CREATE TABLE YourTable 
( 
   Id INT IDENTITY(1,1) PRIMARY KEY, 
     ....... 
); 

Create Id column with primary key

IDENTITY(1,1) property automatically increament your ID column value for each row.

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

Niranjan you add primary key on your table in order to don't create integrity problem.

CREATE TABLE YourTable
(
   Id INT IDENTITY(1,1) PRIMARY KEY,
   .......
);

I suggest you identity treatment, but you can adjust your primary key, it's just additional suggestion

Upvotes: 2

Related Questions