Checkbox as template column in data grid

I have dropped a checkbox into a data grid as a template column in order to show the status of a certain record i.e approved or unapproved. What I need to be able to do now is to update the bit value in the sql table according to the state of the checkbox and vice versa. According to msdn what I need to do is write the script into the html of the aspx which I have done but that script utilises objects found in the namespace so when I drop the following script\code into the html\head section of the aspx page: void checkChangedApproved(Object sender, EventArgs e)
                  SqlCommand checkChangedApproved = new SqlCommand();
                  checkChangedApproved.Connection = sqlConnection1;
                  checkChangedApproved.CommandText = "Update nameoftablehere set approved = '1'";
                  checkChangedApproved.CommandType = CommandType.Text;

I get the following error when I run the code:

Compiler Error Message: CS0246: The type or namespace name 'SqlCommand' could not be found (are you missing a using directive or an assembly reference?)

..which is fair enough, but I can't put using directives into the html of the pages though. So I go back to the aspx.cs page and drop the code in there and declare it as public so the first lines of that method would look like this:

void checkChangedApproved(Object sender, EventArgs e)
                  SqlCommand checkChangedApproved = new SqlCommand(); ....etc....

which works absolutely fine, but then that breaks the rules of encapsulation does it not??? Because the method is now publicly accesible which is not recommended practise (you can tell I've just learnt c#) ? ...oh I forgot to mention I'm using the OnCheckChanged method so some of the checkbox code looks similar to the following:

                                          <asp:CheckBox id="CheckBox1" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "APPROVED") %>' AutoPostBack=True OnCheckedChanged="checkChangedApproved">
                                          </asp:CheckBox> any tips? I'm sure that I've missed something and that there is a more elegant way to code this that sticks to the fundamentals of the programming language to keep things robust and reliable, many thanks in advance for your help


Who is Participating?
laotzi2000Connect With a Mentor Commented:
you can using a namespace in .aspx like this
<%@ Import namespace = "System.Data.SqlClient" %>
Or you can use the full name space to access those class,
for example, instead of
SqlCommand checkChangedApproved = new SqlCommand();
you can use
System.Data.SqlClient.SqlCommand checkChangedApproved = new System.Data.SqlClient.SqlCommand();

In this way, you don't have to using any namespace.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.