Help building SQL update command based on form controls

Hi guys, I have a simple webpage with around 10 controls of various type (TextBox, DropDownList, CheckBox, Etc).

I'm trying to write a function, cco, that formats an output string for me. Basically I want to pass in the control name, and have cco return a preformatted SQL string (Add quote in front of strings, double up any quotes, change true and false to 1 and 0, etc).

In the follow save_item function itmdesc, desc2, and item are all TextBox items.

The problem is when I pass the control name in to to my cco function, I can't figure out how to then reference the controls data (in this example, myControl.Text within function cco). I think I'm missing something simple.

Thanks,
Terry


 protected bool save_item()
    {

        // Build update string
        string lc_cmd = "update icitem set itmdesc = " + cco(itmdesc) + ", desc2 = " + cco(desc2) +
             " where item = " + cco(item);
       
        // Prepare a SQL connected and perform update
        SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
        cn.Open();
        SqlCommand cmd = new SqlCommand(lc_cmd, cn);

        SendEmail("tbur@kretek.com", "tbur@kretek.com", "SQL INV COMMAND", lc_cmd);

        cmd.ExecuteNonQuery();

        cmd.Dispose();
        cn.Dispose();

        return true;

    }


    protected string cco(Control myControl)
    {

        switch (myControl.GetType().ToString().Trim())
        {
            case "System.Web.UI.WebControls.TextBox":
                {
                   
                string lc_string = myControl.ToString().Trim();

                //Double up any single quotes to prepare for SQL update
                string lc_result = "";

                while (lc_string.Length > 0){

                if (lc_string.Substring(0,1) == "'")
                {
                    lc_result = lc_result + "''";
                }
                else
                {
                    lc_result = lc_result + lc_string.Substring(0,1);
                }

                lc_string = lc_string.Substring(1, lc_string.Length - 1);
               
                }


                 return "'" + lc_result.Trim() + "'";
                }
                break;
           
          default :

                Label1.Text = Label1.Text + myControl.GetType().ToString().Trim() + ", ";

                return "''";

        }
           


    }
TerryBurgerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prajeebkumarCommented:
you have to declare a dummy text box inside the function

case "System.Web.UI.WebControls.TextBox":
                {
                 
              TextBox tb = (TextBox)myControl;

                string lc_string = tb.Text;

or string lc_string = ((TextBox)myControl).Text  (i'm not sure if its the exact syntax, but the one above would work)
athapaCEO / CTOCommented:
You need to use .Text property of TextBox otherwise it will just display the class name.

    protected string cco(Control myControl)
    {

        switch (myControl.GetType().ToString().Trim())
        {
            case "System.Web.UI.WebControls.TextBox":
                {
                   
                //string lc_string = myControl.ToString().Trim();
                 string lc_string = myControl.Text.Trim();
 
Same thing for default.

AT
TerryBurgerAuthor Commented:
I get the following error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0117: 'System.Web.UI.Control' does not contain a definition for 'Text'

Source Error:

 

Line 434:                {
Line 435:                    
Line 436:                string lc_string = myControl.Text.Trim();
Line 437:
Line 438:
 

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

TerryBurgerAuthor Commented:
When I try the first solution, I get

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0118: 'System.Web.UI.WebControls.TextBox' is a 'type' but is used like a 'variable'

Source Error:

 

Line 434:                {
Line 435:
Line 436:                TextBox tb = TextBox(myControl);
Line 437:
Line 438:                string lc_string = tb.Text.Trim();
 
prajeebkumarCommented:
it should be,

TextBox tb = (TextBox) myControl; // bracket around (TextBox)
Line 437:
Line 438:                string lc_string = tb.Text.Trim();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TerryBurgerAuthor Commented:
Thanks!! What is the overhead involved in creating extra TextBox objects like this? Should I be worried about it?
prajeebkumarCommented:
yes, there is an overhead in creating that dummy textbox.. thats why I mentioned the other aspect.. i wasn't sure abt the syntax so i wrote a small program to check it.. here it is..

      
                  string lc = ((TextBox)myControl).Text;

You have to cast it to a textbox since myControl is a general control object inside the function. This way you don't create another object explicitly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.