0

I write a statement for updating details belongs to primary key(Mobile). but it is working only for other columns. when i update mobile number. it doesn't change.

Here the my query

private void button4_Click(object sender, EventArgs e)
{
    con.Open();

    SqlCommand cmd = new SqlCommand(@"UPDATE [dbo].[Table]
SET [First] = '"+textBox1.Text+"',[Last] = '"+textBox2.Text+ "' ,[Mobile] = '" +textBox3.Text+ "'    ,[Email] = '" +textBox4.Text+ "' ,[Category] = '" + comboBox1.Text + "' WHERE (Mobile='" + textBox3.Text + "')", con);

    cmd.ExecuteNonQuery();

    con.Close();
    MessageBox.Show("Updated Successfully");
    display();
}

These following columns can be updated

First,
Last,
Email,
Category

but Mobile column cannot be updated.

CREATE TABLE [dbo].[Table]
(
    [First] VARCHAR(50) NOT NULL , 
    [Last] VARCHAR(50) NOT NULL, 
    [Mobile] VARCHAR(50) NOT NULL, 
    [Email] VARCHAR(50) NOT NULL, 
    [Category] VARCHAR(50) NOT NULL, 

    CONSTRAINT [PK_Table] PRIMARY KEY ([Mobile])
)

Could anybody tell me the error?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3398379
  • 83
  • 1
  • 8
  • Can you share the table definition? – Afonso Jun 29 '18 at 16:47
  • Perhaps there is a trigger on the table that prevents updating the field or your database has column-level permissions. – Gordon Linoff Jun 29 '18 at 16:47
  • i added table definition – user3398379 Jun 29 '18 at 16:49
  • Is there a foreign key on the Mobile column? If so, what is the update rule of that foreign key? – Paul Williams Jun 29 '18 at 16:51
  • there are only primary key.. – user3398379 Jun 29 '18 at 16:52
  • I suggest you to try to update a record directly on the database (SQL Server Management Studio or console for example) and post the query resultls. – Máster Jun 29 '18 at 16:55
  • And you're sure the Mobile number is exactly the same between the text box and the table (no leading/trailing spaces?) And you should also read up on SQL Injection. – Duston Jun 29 '18 at 17:00
  • 2
    [SQL Injection alert](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - you should **not** concatenate together your SQL statements - use **parametrized queries** instead to avoid SQL injection - check out [Little Bobby Tables](http://bobby-tables.com/) – marc_s Jun 29 '18 at 17:02
  • Aside from the huge issue of sql injection you seem to have some design challenges here. When you get to a point where you need to start updating primary keys you need to take a step back and rethink your data structures. You have to update not just this table but any other tables referencing this one. And if you (hopefully so) have foreign keys on those child tables you have a bit of a challenge. You can't update the child because there is no parent value. And you can't update the parent because the child would become invalid. – Sean Lange Jun 29 '18 at 19:29

4 Answers4

0

Your update to [mobile] doesn't make any sense because of the where clause. It states: "update all this stuff including the mobile where the mobile is this value. You're using textbox3.Text in both cases so it will never update because it is the same value.

e.g.

update foo set mobile = 'abc' where mobile = 'abc'

mobile will never be anything apart from 'abc' given that query.

If you want to fix this problem you need to actually design your table properly. Have a separate primary key:

PersonId int identity primary key  

then you can do:

update Person set mobile = 'abc' where PersonId = 1  

and store the PersonId somewhere where the user doesn't have to look at it.
If you're gonna query the table via Mobile a lot then consider adding a secondary index onto the Mobile column, if you need it to be unique add a unique constraint as well.

Also, have a GOOD read through WinForm DataBinding in general as the way your going around this is going to cause you problems later on. BindingContext is an important class to understand but will take some time to do so. You'll make this time back later IN SPADES, so do read on it.

Quibblesome
  • 25,225
  • 10
  • 61
  • 100
  • @user3398379, you should simply remove the mobile from the set of updated fields AND MORE IMPORTANTLY, drop using this style of string concatenation and use parameters. It was your luck to get the error this way. – Cetin Basoz Jun 29 '18 at 17:05
0

It's kinda strange that you want to update your primary key rather than insert a new record. Anyway, check this answer that talks about Disabling constraint enforcement:

https://stackoverflow.com/a/2499328/1821637

Máster
  • 981
  • 11
  • 23
0

In order to update the mobile number, you need to add an additional text box or otherwise supply the old mobile number.

If you are using a WPF or Windows form, then I would add a new form field for the old mobile number and pass that field to your where clause. This will work provided the old mobile number is an exact match. Given that you are doing varchar(50) then you may get errors for people who entered the data differently, such as (555) 555-5555 versus 555.555.5555.

SQ LoBue
  • 11
  • 3
0

As said already it doesn't make any sense to update the field to the same value you do the search on. Also you should never write SQL statements like that but use parameters:

private void button4_Click(object sender, EventArgs e)
{


SqlCommand cmd = new SqlCommand(@"UPDATE [dbo].[Table] SET 
      [First] = @first,
      [Last] = @last, 
      [Email] = @email,
      [Category] = @cat
  WHERE (Mobile=@mobile)", con);

cmd.Parameters.Add("@first", SqlDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@last", SqlDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = textBox4.Text;
cmd.Parameters.Add("@cat", SqlDbType.VarChar).Value = comboBox1.Text;
cmd.Parameters.Add("@mobile", SqlDbType.VarChar).Value = textBox3.Text;

con.Open();
cmd.ExecuteNonQuery();
con.Close();

MessageBox.Show("Updated Successfully");
display();
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • What does "nope still not working" mean? What is the error message? Does that mobile number you enter exists? BTW, it would be easier if you used LINQ. – Cetin Basoz Jun 29 '18 at 17:20
  • Oh my :) There is nothing to update there. You are searching by mobile number and updating other fields. If you need to update mobile number, then you should include it in the update list PLUS make sure you don't have any other constraints that prevent that. Otherwise you would need to disable\enable constraint checking (Using mobile number as the primary key is not trivial for the novice). – Cetin Basoz Jun 29 '18 at 17:28
  • i change my project and got a Auto incrementing ID,Then set that ID as primary key. now work is done – user3398379 Jun 30 '18 at 06:11