Link to home
Start Free TrialLog in
Avatar of lesmydad
lesmydad

asked on

SQL UPDATE query

Hi Guys,
I'm trying to update fields within the database. The ones i'm tring to update are the cekeck boxes. When clicked i want the check boxes to change. I've doent his query but keep on getting an error with the checkChanged event handler. Can anyone direct me on how i could fix the problem:

private void button5_Click(object sender, System.EventArgs e)
            {DataSet ds = new DataSet();
                  SqlConnection con = new SqlConnection (@"Data Source= xxx.xxx.xxx.xxx;"+
                        "Initial Catalog = drmTest;" +
                        "Network Library=DBMSSOCN;"+
                        "user id =xx;"+
                        "Password=xx"); // assign conString
                  String sQuery = "SELECT * FROM License WHERE Name = '"+ textBox2.Text +"' AND ContentOwnerID = '"+ textBox3.Text +"'";
                  String sUpdate = "UPDATE AllowBackupRestore, AllowBurnToCD, AllowPlayOnCD" + "VALUES ('"+ checkBox1.CheckedChanged +"','"+ checkBox2.CheckedChanged +"' )";
                  Console.WriteLine (sQuery); // to get the query for debug, you can try execute this query in sql query analyser
                  SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sQuery, con);

                  sqlDataAdapter1.Fill(ds, "License");
                  Form2 frm = new Form2();
                  frm.dataGrid1.DataSource = ds.Tables["License"].DefaultView;
                  tabPage2.Show();
                  con.Close();
}
Avatar of jkrill
jkrill

CheckChanged is an EventHandler, and not a property.  You can capture this event in your constructor by adding a line like the following:

checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

and then creating a method that handles it:

private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
// this will be called whenever the checkbox state has been changed.
}

If you want to determine whether the box is currently checked or not, you use the Checked property:

if(checkBox1.Checked)
{
// check box is selected
}
else
{
// check box is not selected.
}

If  you want to determine if it has just been changed, you'll have to store the value elsewhere and compare it:

bool check1InitialState = checkBox1.Checked;

Then elsewhere:

if(check1InitialState != checkBox1.Checked)
{
// Checkbox was changed.
}
Avatar of lesmydad

ASKER

Hi jkrill
I've already got the method called. I'm having problem with the UPDATE string. I'm trying to update the databse field of ckeck boxes via update queries. When the checkboxes are clicked or un checked i want the action also to take place in the database.
Thanks
Les
Just to clarify, do you want the database values to be change as soon as the checkbox values are changed, or not until after a button is clicked?  Right now it appears you're handling it when a button is clicked.  Either way, you can't use checkBox1.CheckChanged in the manner you currently are.  You'll need to use checkBox1.Checked.
Yes i wan the database values to change when the checkboxes values are changed. These changes should take place whne i click the update button.
Thanks
SOLUTION
Avatar of jkrill
jkrill

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jkrill,

I have a SQL database (through remote connection) table, i'm developing a software where it is being intergrated with sql database. At present when a user click a button they are able to view thier details (login is required). Now i want to let the user update thier detail (again fileds to update will be chossen via login details). All fields will be shown in a dataGrid. I have checkboxes in that database. If a user wants to change the values of the checkboxex (ticks), they will first amend that value, then when they press a button (update), automatically these values will change in teh database, and when they press the view butoon they will be able to see these changes.
I've produced the above script, but i know theres something wrong with the Update string, thats where the error keeps on popping up.

String sQuery = "SELECT * FROM License WHERE Name = '"+ textBox2.Text +"' AND LicenseID = '"+ textBox3.Text +"'";

String sUpdate = "UPDATE AllowBackupRestore, AllowBurnToCD" + "VALUES ('"+ checkBox1.Checked +"','"+ checkBox2.Checked +"' )";

Console.WriteLine (sUpdate); // to get the query for debug, you can try execute this query in sql query analyser
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sUpdate, con);
 sqlDataAdapter1.Fill(ds, "License");
                  Form2 frm = new Form2();
                  frm.dataGrid1.DataSource = ds.Tables["License"].DefaultView;
                  frm.Show();
                  con.Close();

Can anyone figure out how i could solve this problem. Will prefer if they amended the above script to show thw changes.
Thanks,
Les
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry not statements.. but i mean words..
Hi Karra,
the above code bring this error:
System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near ','.
I'm not sure where this section relates to, i'm not calling any exception at present.
Is there anything wrong with the update string, or aer the methods are being called correctly.
Thanks
try to modify your SQL like this

String sUpdate = "UPDATE License(AllowBackupRestore, AllowBurnToCD, AllowPlayOnCD) VALUES(" + checkBox1.Checked.ToString() + "," +
checkBox2.Checked.ToString() + "," +
checkBox3.Checked.ToString() + ")";

One obvious error to me is that your UPDATE SQL statement is wrong, you need to write the UPDATE as

UPDATE tablename(col1, col2, ...) VALUES(val1, val2, ...)

You have three values to update, then you need to provide three.

Since I don't know the table schema, I'm not sure if the data conversion of values is right.  Maybe you give a try and let me know.
I agree with artyx!
Your UPDATE  query is wrong!
Have you tried all experts' suggestions?
Ok Guys,
This is causing confusion amongst us. I'll now change the whole scenario an dmake it easy.
I'm not going to use dataGrid anymore. I now have these textBoxes, i want to retrive the information from the database to the approipate textBox. Then through these textBox i will want to update the fields.
First can some one tell me what scritpting will i need to insert in order to retrive data from the database to these textboxes, i have three textBoxes. How do i show the results in the textBoxes:

SqlConnection con = new SqlConnection (@"Data Source= xxx.xxx.xxx.xxx;"+
                   "Initial Catalog = drmTest;" +
                   "Network Library=DBMSSOCN;"+
                   "user id =xx;"+
                   "Password=xxxxxxxx"); // assign conString
                  String sQuery = "SELECT Name, Description, Price FROM License WHERE Name = '"+ textBox5.Text +"' AND LicenseID = '"+ textBox6.Text +"'";


Then when they are retrived, if any editing is done, another button will be used fore the updates.

Thanks
Les

Found the Answer
String sQuery = "SELECT Name, Description, Price FROM License WHERE Name = '"+ textBox5.Text +"' AND LicenseID = '"+ textBox6.Text +"'";
            //Console.WriteLine(sQuery);
                  SqlCommand cmd = new SqlCommand(sQuery, con);
                  //SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sQuery,con);
                  con.Open();
                  SqlDataReader dr = cmd.ExecuteReader();
                  
                  if( dr.Read() )
                  {
                        textBox1.Text = dr[ "Name" ].ToString();
                        textBox2.Text = dr[ "Description" ].ToString();
                        textBox3.Text = dr[ "Price" ].ToString();
                        // ......
                  }
                  dr.Close();

Thanks Anyway
Les
Hi Guys,
need help again, How would i retrive the attribute of a checkBox and radio button. In the database these checkBoxes and radio button is defined by 1 and 0 which means true/false. If checkbox value is 1 then the database in the form will be checked to indicate its true and vice versa.

I tried:     checkBox1.Checked = dr[ "expiry" ].ToString();
but this send string error.

Any one has any idea how i go through this hurdle.
Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What about for radioButton, where i have either Static or Dynamic instead of 1 and 0
Can someone also help me with the update script now, its much clearer then before. After retriving all the filds from the database to its approipate textBox, checkBox etc if i make a change tot any of the text or any other filrds, after pressing update i want that effect to take place in the database as well as the form. I wrote this script but having problem with the update section:

String sUpdate = "Update * FROM License SET Name = '" + checkBox1.Checked.ToString() + "', AllowPlayOnPC ='" + checkBox1.Checked + "'"  +
                        "WHERE Name = '"+ textBox5.Text +"' AND LicenseID = '"+ textBox6.Text +"'";
                  SqlCommand cmd = new SqlCommand(sUpdate, con);
                  con.Open();
                  SqlDataReader dr = cmd.ExecuteReader();
                  con.Close();

Thanks
Les
I will split the points amongst these who have given me an answer which functioned.
Thanks
Sorted, found the answer:
String sUpdate = "Update License SET Name = '" + textBox1.Text.ToString() + "'" +
                        " WHERE Name = '"+ textBox5.Text +"' AND LicenseID = '"+ textBox6.Text +"'";
                  SqlCommand cmd = new SqlCommand(sUpdate, con);
                  con.Open();
                  cmd.ExecuteNonQuery();
                  con.Close();
How will i update the changes to the checkBox and radioButtons. With them its true and false which determines the event. I tried using:
AllowPlayOnPC ='" + checkBox1.Checked + "' - But this give error of true and fals enot declared properly. What will go after
checkBox1.Checked  to convert into Int (1 and 0).

Also still having problem with declaring the radio button event where i have either Static or Dynamic instead of 1 and 0, how will i declare this in the form and the database.

Thanks
Les