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?
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)
0
 
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
0
 
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:
 

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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();
 
0
 
prajeebkumarCommented:
it should be,

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

Experts Exchange Solution brought to you by ConnectWise

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?
0
 
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.
0
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.