Solved

Dynamically creation of textBox

Posted on 2010-09-14
7
393 Views
Last Modified: 2013-12-16
Experts
My question is
how to create dynamically textboxes and how those textboxes will pass values to queries.
For clarification of my question please see the attached image.

hoping for a vivid explanation as I have never done so.
Thanking you,
error.JPG
0
Comment
Question by:ANINDYA
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:kris_per
Comment Utility

Some questions to understand it better:

For example, when the user selects 'Opp By Title', the query is:
select * from Table_Opportunity where opportunity_title like '%[title]%'
For this query, the number of text boxes to create is ONE. Is that right?

Can give you an example for the case of two text boxes?

Why do you create the textboxes/to use where?


0
 

Author Comment

by:ANINDYA
Comment Utility
Expert kris per
Thanks for replying.

Opp by Title and Location
Opp by multiple locations ( at that time atleast 4 locations will be an option for the user to put an input)
Opp by CompanyName and Location
Opp by Price range ( At that time atleast 2 textboxes  are required)
Opp by Date ( two dates)

in this way many more will be there .
I am first trying to execute the location option only .
So that I can understand  the concept and then the same later I can use on all other queries.

Thanking you

0
 
LVL 16

Expert Comment

by:kris_per
Comment Utility

Can you confirm the following:

For 'Opp by Title and Location', the query will be like:

select * from Table_Opportunity where opportunity_title like '%[title]%' and Location like '%[title]%'

i.e. the query will have an extra 'and Location like '%[title]%' .

Is that correct?

Basically I am trying to understand, number of textboxes depends on what?

Is it like - if the query has two field names in the where expression, then the number of text boxes is two. Can this be taken as a rule. Will it work for you?

And again, how the textboxes will be used after created? Should their value used for some purpose?


0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:ANINDYA
Comment Utility
Expert kris_per
number of textboxes depends on number of input parameters only . Not necessarily they are supposed to have a title name only .
Those parameters can be title,location,date,etc.
I do not know how to do that as they will have separate datatypes .
But sir it is requirement
Thanking you
0
 

Author Comment

by:ANINDYA
Comment Utility
Expert kris_per
I am from India.here my office is closing now. So furthermore I will not be able to reply you instantly.
Tomorrow morning I will again come at ( USA local time night 10PM).
So please do not think I am not replying  you.
Thanking you
Anindya
0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
Comment Utility

OK. No problem.

Hope the below code is getting you closer to the solution....Let me know for explanation on the code.
public partial class Form1 : Form

    {

        private string connectionString = "your connection string here";

        private DataTable dataTable;

        private List<TextBox> textBoxes = new List<TextBox>();

        private List<Label> labels = new List<Label>();



        public Form7()

        {

            InitializeComponent();



            // Get the query details from database

            // and databind to comboBoxOpportunityCategories 

            using (SqlConnection connection = new SqlConnection(connectionString))

            {

                connection.Open();



                SqlCommand c = new SqlCommand("SELECT * FROM Table_Query", connection);



                SqlDataAdapter a = new SqlDataAdapter(c);



                dataTable = new DataTable();



                a.Fill(dataTable);

            }



            comboBoxOpportunityCategories.DataSource = dataTable;

            comboBoxOpportunityCategories.DisplayMember = "QDesc";

            comboBoxOpportunityCategories.ValueMember = "Query";

        }        



        private void comboBoxOpportunityCategories_SelectedIndexChanged(object sender, EventArgs e)

        {

            string query = comboBoxOpportunityCategories.SelectedValue.ToString();

            //string query = @"select * from Table_Opportunity where opportunity_title like '%[title]%' and Location like '%[loc]%' and testfield like '%[test]%'  and testfield2 like '%[test2]%'";



            List<string> fieldList = GetFieldListFromQuery(query);



            // clear previous text boxes and labels in the form

            foreach (TextBox t in textBoxes)

            {

                this.Controls.Remove(t);

            }

            foreach (Label l in labels)

            {

                this.Controls.Remove(l);

            }



            // start the textboxes from the same Y and to the left of comboBoxOpportunityCategories

            int startY = comboBoxOpportunityCategories.Location.Y;

            int startX = comboBoxOpportunityCategories.Location.X + comboBoxOpportunityCategories.Width + 10;



            int controlStartX = startX;

            int controlStartY = startY;

            int textBoxesPerLine = 3;

            int textBoxWidth = 100;

            int labelWidth = 40;



            for (int f = 0; f < fieldList.Count; f++)

            {

                // add a label before textbox

                Label label = new Label();

                label.Location = new System.Drawing.Point(controlStartX, controlStartY+3);

                label.Name = "labelField" + (f + 1);

                label.AutoSize = false;

                label.Size = new System.Drawing.Size(labelWidth, 20);

                label.Text = fieldList[f] + ":";

                this.Controls.Add(label);

                labels.Add(label);



                controlStartX += label.Width;



                // add the textbox for the field

                TextBox textBox = new TextBox();

                textBox.Location = new System.Drawing.Point(controlStartX, controlStartY);

                textBox.Name = "textBoxField" + (f+1);

                textBox.Size = new System.Drawing.Size(textBoxWidth, 20);

                textBox.Tag = fieldList[f];

                this.Controls.Add(textBox);

                textBoxes.Add(textBox);



                if ((f + 1) % textBoxesPerLine == 0)

                {

                    controlStartX = startX;

                    controlStartY += 25;

                }

                else

                {

                    controlStartX += textBox.Width + 5;

                }

            }

        }



        private List<string> GetFieldListFromQuery(string query)

        {

            // Logic used here is: The text within a pair of % is the field name.

            // may be better logic can be used later; but for now this works



            string[] splits = query.Split('%');

            List<string> fieldList = new List<string>();

            for (int f = 0; f < splits.Length; f++)

            {

                if (f % 2 == 1)

                {

                    fieldList.Add(splits[f]);

                }

            }



            return fieldList;

        }



        // When user clicks the Find button after typing values in the textboxes



        private void buttonFind_Click(object sender, EventArgs e)

        {

            string query = comboBoxOpportunityCategories.SelectedValue.ToString();

            //string query = @"select * from Table_Opportunity where opportunity_title like '%[title]%' and Location like '%[loc]%' and testfield like '%[test]%'  and testfield2 like '%[test2]%'";



            // replace the tokens like [title], etc in the query with the values from the textbox

            // to make the full query to use in database.

            foreach (TextBox textBox in textBoxes)

            {

                query.Replace(textBox.Tag.ToString(), textBox.Text);

            }



            DataTable dataTableResults = null;



            using (SqlConnection connection = new SqlConnection(connectionString))

            {

                connection.Open();



                SqlCommand c = new SqlCommand(query, connection);



                SqlDataAdapter a = new SqlDataAdapter(c);



                dataTableResults = new DataTable();



                a.Fill(dataTableResults);

            }



            // now dataTableResults has the results of the query

            // use dataTableResults as required

        }

    }

Open in new window

0
 

Author Closing Comment

by:ANINDYA
Comment Utility
Expert kris_per
Thanks a lot for the code .
It has really provided me a lot of knowledge and there after I arranged the things properly.

Thanks for the help.
Hope to get the same in future too.
Thanking you,
Anindya Chatterjee
Bangalore
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now