?
Solved

Help building SQL update command based on form controls

Posted on 2006-04-04
7
Medium Priority
?
817 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 "''";

        }
           


    }
0
Comment
Question by:TerryBurger
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:prajeebkumar
ID: 16377233
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
 
LVL 10

Expert Comment

by:athapa
ID: 16377252
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
 

Author Comment

by:TerryBurger
ID: 16377273
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TerryBurger
ID: 16377286
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
 
LVL 3

Accepted Solution

by:
prajeebkumar earned 2000 total points
ID: 16377299
it should be,

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

Author Comment

by:TerryBurger
ID: 16377326
Thanks!! What is the overhead involved in creating extra TextBox objects like this? Should I be worried about it?
0
 
LVL 3

Expert Comment

by:prajeebkumar
ID: 16377378
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

840 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