We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Help building SQL update command based on form controls

TerryBurger
TerryBurger asked
on
Medium Priority
831 Views
Last Modified: 2008-02-01
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 "''";

        }
           


    }
Comment
Watch Question

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 / CTO
Top Expert 2005

Commented:
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

Author

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:
 

Author

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks!! What is the overhead involved in creating extra TextBox objects like this? Should I be worried about it?
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.