Checkbox as template column in data grid

Posted on 2005-03-25
Medium Priority
Last Modified: 2010-04-07
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 system.data.sqlclient 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">

..so 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


Question by:ramrod1979
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 12

Accepted Solution

laotzi2000 earned 750 total points
ID: 13631582
you can using a namespace in .aspx like this
<%@ Import namespace = "System.Data.SqlClient" %>
LVL 12

Expert Comment

ID: 13631605
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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question