1 Replies - 426 Views - Last Post: 03 January 2017 - 11:33 PM

#1 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Visual studio and sql problem The CREATE UNIQUE INDEX

Posted 03 January 2017 - 11:06 PM

Hey guys so I got a annoying problem all day my databse won't update I get this error:

The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.Stocks' and the index name 'PK_Stocks'. The duplicate key value is (123).

And I got this other problem I believe its because my database won't update properly. If I select type one value in text box to update EVERY other value gets updated aswell:

This is a picture might help:

Posted Image

This is code:

        private void button4_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(@"UPDATE Stocks SET CompanyName = '"
+ textBox1.Text + "', DE = '" + textBox2.Text + "', ROE = '"
+ textBox3.Text + "', ROA = '" + textBox4.Text + "', CompanyNamPE =  '" + textBox6.Text +
 "' WHERE (ROE = '" + textBox3.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();

            MessageBox.Show("Updated successfully");
            Display();
        }



and this is the display method:

      void Display()
        {
            SqlDataAdapter sda = new SqlDataAdapter("Select * from Stocks" , con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.Rows.Clear();
            foreach(DataRow item in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = item["CompanyName"].ToString();

                dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
                dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
                dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
              dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();

            }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Visual studio and sql problem The CREATE UNIQUE INDEX

#2 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Visual studio and sql problem The CREATE UNIQUE INDEX

Posted 03 January 2017 - 11:33 PM

Dw guys fixed it!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1