2016-09-30 3 views
0

Так что я написал что-то вроде редактора, но у меня проблема с изменением стоимости в каждой строке в одном столбце. Я хочу изменить только одну конкретную ячейку не всех. Также я использовал точки останова, и я понимаю, что это происходит, когда SqlCommand определяется и выполняется, но я не знаю, почему это происходит? Я хочу, например, имеют два значения одно и то же, но и хотят это просто изменить значение в строке, выбравшей в comboxo не во всех строках ...SQL, DataTable. Почему значение меняется в каждой строке в столбце?

private void button1_Click(object sender, EventArgs e) 
    { 
     UpdateBase(); 
     this.Close(); 
    } 

    public string currentvalue = ""; 

    public void UpdateBase() 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30"); 
     if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 0) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 0); 

     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 1) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 1); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 1); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 1); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 2) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 2); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 2); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 2); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 3) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 3); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 3); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 3); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 4) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 4); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 4); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 4); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 5) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 5); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 5); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 5); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 6) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 6); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 6); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 6); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 7) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 7); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 7); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 7); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 8) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 8); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 8); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 8); 
     } 
     else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 9) 
     { 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 9); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 9); 
      setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 9); 
     } 

    } 




SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30"); 
public void setRadioButtonText(RadioButton RadioButton,string RadioButtonColumn, int row) 
{ 

    ////Determine which radiobutton in datatable's row is selected 
      //!// 
    if (RadioButton.Checked == true) 
    { 
      //!// 
     RadioButton.Text = textBox1.Text; 
     ////GET CURRENT VALUE 
     SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Pitanja", con); 
     DataTable dt = new DataTable(); 
     ad.Fill(dt); 
           //!//  //!// 
     currentvalue = dt.Rows[row][RadioButtonColumn].ToString(); 
     ////GET CURRENT VALUE 
                     //!//           //!// 
     SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'", con); 
     comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text); 

     try 
     { 
      con.Open(); 
      comnd.ExecuteNonQuery(); 
     } 
     catch (SqlException ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 
} 

Picture of the problem

ответ

2

Ваша проблема заключается в этой части вашего кода

currentvalue = dt.Rows[row][RadioButtonColumn].ToString(); 
    ////GET CURRENT VALUE 
                    //!//           //!// 
SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'", con); 
    comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text); 

Вы получить значение radiobuttoncolumn и обновить все «Pitanja», где величина radiobuttoncolumn равна этому значению. Поскольку все ваши строки в базе данных равны этому значению. Все они будут обновлены.

Вы должны выбрать уникальное значение для фильтрации вашего обновления, например, столбца идентификатора. Тогда будет обновляться только строка с этим уникальным идентификатором. Вероятно, это немного похоже на это, но я не испытал этого.

currentvalue = dt.Rows[row]["Id"].ToString(); 
    ////GET CURRENT VALUE 
                    //!//           //!// 
SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = @textboxtext WHERE Id = " + currentvalue, con); 
    comnd.Parameters.AddWithValue("@textboxtext", textBox1.Text); 
+0

Я пытался, он не работает с Ид ... Tha nks в любом случае! –

+2

@HuryH _ «это не работает» _ Это неопределенно. Что он делает? Выбросить ошибку? Обновите базу данных каким-то неожиданным способом? Кто-то тратит время, чтобы попытаться помочь вам, поэтому предоставьте как можно больше деталей. –

+0

Какое исключение вы видите? Или что вы говорите, что это не работает? – HansVG

0

Я попытался код, отправить HansVG, но не заметил, что HansVG Сайед «Поскольку все ваши строки в базе данных, равным этому значению. Все они будут обновлены.», Так что я не пытался с Id до он работал ...

Вот как это выглядит:

private void button1_Click(object sender, EventArgs e) 
{ 
    UpdateujBazu(); 
    this.Close(); 
} 

public string currentvalue = ""; 

public void UpdateujBazu() 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30"); 
    if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 0) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 0,1); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 0,1); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 0,1); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 1) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 1,2); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 1,2); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 1,2); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 2) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 2,3); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 2,3); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 2,3); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 3) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 3,4); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 3,4); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 3,4); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 4) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 4,5); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 4,5); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 4,5); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 5) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 5,6); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 5,6); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 5,6); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 6) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 6,7); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 6,7); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 6,7); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 7) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 7,8); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 7,8); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 7,8); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 8) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 8,9); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 8,9); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 8,9); 
    } 
    else if (Povezivac_varijabli_Formama.edi.comboBox1.SelectedIndex == 9) 
    { 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton1, "RadioBtn1text", 9,10); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton2, "RadioBtn2text", 9,10); 
     setRadioButtonText(Povezivac_varijabli_Formama.edi.radioButton3, "RadioBtn3text", 9,10); 
    } 

} 




SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\JM\Documents\Kviz.mdf;Integrated Security=True;Connect Timeout=30"); 
public void setRadioButtonText(RadioButton RadioButton,string RadioButtonColumn, int row,int ID) 
{ 

    ////Determine which radiobutton in datatable's row is selected 
      //!// 
    if (RadioButton.Checked == true) 
    { 
      //!// 
     RadioButton.Text = textBox1.Text; 
     ////GET CURRENT VALUE 
     SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Pitanja", con); 
     DataTable dt = new DataTable(); 
     ad.Fill(dt); 
           //!//  //!// 
     currentvalue = dt.Rows[row][RadioButtonColumn].ToString(); 
     ////GET CURRENT VALUE 
                     //!//           //!// 
     SqlCommand comnd = new SqlCommand("UPDATE Pitanja SET " + RadioButtonColumn + " = '" + textBox1.Text + "'" + " WHERE " + RadioButtonColumn + " LIKE '" + currentvalue + "'" + " AND Id = '" + ID + "'", con); 

     try 
     { 
      con.Open(); 
      comnd.ExecuteNonQuery(); 
     } 
     catch (SqlException ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 
} 

Picture

Смежные вопросы