PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Trying to convert a String variable to TextBox?-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] Trying to convert a String variable to TextBox?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,922

    Resolved [RESOLVED] Trying to convert a String variable to TextBox?

    I have 6 sets of TextBox controls with 4 different naming schemes, ie;

    These are textbox controls on my webpage.

    Set 1
    txtFName1
    txtLName1
    txtEmail1
    txtPhone1

    Set 2
    txtFName2
    txtLName2
    txtEmail2
    txtPhone2
    .
    .
    .
    and so forth.

    What I am trying to do is use a For Loop to add all these TextBox values to a database. So in my For Loop I would try something like this:

    Code:
                Int cnt = valueHolder.value;
    
                string txtFName = "txtFName";
                string txtLName = "txtLName";
                string txtEmail = "txtEmail";
                string txtPhone = "txtPhone";
                TextBox tFName = new TextBox();
                TextBox tLName = new TextBox();
                TextBox tEmail = new TextBox();
                TextBox tPhone = new TextBox();
    
                SqlCommand cmd;
    
                for (var x = 0; x < cnt; x++)
                {
                    txtFName = txtFName + (x + 1);
                    txtLName = txtLName + (x + 1);
                    txtEmail = txtEmail + (x + 1);
                    txtPhone = txtPhone + (x + 1);
    
                    tFName.ID = txtFName;
                    tLName.ID = txtLName;
                    tEmail.ID = txtEmail;
                    tPhone.ID = txtPhone;
    
                    cmd = new SqlCommand("", GetConnection());
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spInsertGuests";
                    cmd.Parameters.Add("@eventID", valueHolder.Value);
                    cmd.Parameters.Add("@startDate", startDate.Value);
                    cmd.Parameters.Add("@endDate", endDate.Value);
                    cmd.Parameters.Add("@FName", tFName.Text);
                    cmd.Parameters.Add("@LName", tLName.Text);
                    cmd.Parameters.Add("@email", tEmail.Text);
                    cmd.Parameters.Add("@phone", tPhone.Text);
    
                    retCode = cmd.ExecuteNonQuery();
                }
    I hope this makes sense! I'm just trying to eliminate alot of code.

    Thanks,
    Blake

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Trying to convert a String variable to TextBox?

    Check out your form's Control.Find method.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,922

    Re: Trying to convert a String variable to TextBox?

    Wild_Bill,

    I already tried that. I still couldn't get it to work.
    Blake

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,177

    Re: Trying to convert a String variable to TextBox?

    Are the textboxes in any container? Example: a group box? If so, you'll need to loop through the controls of the group box and not the form to find the controls.

    For example, if you have a groupbox with a "textBox1" textbox in it, the code below will show "textBox1" and "1". If the control was not found, the "1" would be a "0".

    C# Code:
    1. foreach(Control control in groupBox1.Controls)
    2. {
    3.    MessageBox.Show(control.Name);
    4.  
    5.    MessageBox.Show("" + groupBox1.Controls.Find("textBox1", false).Count());
    6. } //end foreach

    If however, you referenced the form, and did the below. It would list the groupbox, but not the textbox within the groupbox

    C# Code:
    1. foreach(Control control in this.Controls)
    2. {
    3.   MessageBox.Show(control.Name);
    4. } //end foreach

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,922

    Re: Trying to convert a String variable to TextBox?

    They are within a <div>.
    Blake

  6. #6
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,177

    Re: Trying to convert a String variable to TextBox?

    Yeah, a div is another container. So you'll need to loop through the controls of the div, and not the form.

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,038

    Re: Trying to convert a String variable to TextBox?

    Perhaps you need something like "getElementsByTagName" or something similar if the Textboxes are in a webpage.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,922

    Re: Trying to convert a String variable to TextBox?

    I ended up doing it the long way only because I don't have the time to put into it...thanks guys!
    Blake

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,038

    Re: [RESOLVED] Trying to convert a String variable to TextBox?

    What do you mean the long way?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,922

    Re: [RESOLVED] Trying to convert a String variable to TextBox?

    There are 24 textbox fields on this page. 6 sets of 4 different textboxes. Each set has a number as the last digit. So, if I could assign a variable the name of each TextBox set, I could use a For loop and increment each set. It's just a time saver.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width