Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi everyone
I have some TextBox and Lable. I want to put integer variable in part of TextBox's name.
for example:

C#
private void btnSabtClerkEvaluation_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbl1MadrakeTahsili1.Text.Trim() == string.Empty || lbl2Takhasos2.Text.Trim() == string.Empty || lbl3Khalaghiat3.Text.Trim() == string.Empty || lbl4Nazm4.Text.Trim() == string.Empty || lbl5Raftar5.Text.Trim() == string.Empty ||
                   lbl6Taamol6.Text.Trim() == string.Empty || lbl7Masouliatpaziri7.Text.Trim() == string.Empty || lbl8Arastegi8.Text.Trim() == string.Empty || lbl9MizaneKhata9.Text.Trim() == string.Empty || lbl10SakhtiKar10.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    SqlCommand cmd = new SqlCommand("ClerkEvaluation_Insert", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    for (int i = 0; i <= 10; i++)
                    {

                        cmd.Parameters.AddWithValue("@clrk_Name", txtNameClerkEvaluation.Text);
                        cmd.Parameters.AddWithValue("@clrk_Family", txtFamilyClerkEvaluation.Text);
                        cmd.Parameters.AddWithValue("@clrk_MelliCode", txtMelliCodeClerkEvaluation.Text);

                        cmd.Parameters.AddWithValue("@clrke_Percent", lbl[i].Text);// this name is lbl1. How to give number?
                        cmd.Parameters.AddWithValue("@clrke_Tozihat", txt[i].Text);// This name is lbl2
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
            }
}



I want to send a number of Contorols on the form to sql and sql to do own order base on this number. I exactly want to determine when which one of lables are sending now. I should define lbl[i], because with 'i' i can send all Lable.Text or TextBox.Text order. I want to say lbl[i] (=lbl1) .Text to send now.but i have tried

C#
cmd.Parameters.AddWithValue("@clrke_Percent", lbl[i].Name.Substring(3) + " " + lbl[i].Text);
cmd.Parameters.AddWithValue("@clrke_Tozihat", txt[i].Name.Substring(3) + " " + txt[i].Text);


and
SQL
When i write this line, I get this error
"Error2The name 'lbl' does not exist in the current contextD:\Project\evaluation\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs976119WindowsFormsApplication1"
Thx for your help.
Posted
Comments
Hemant Singh Rautela 23-Jan-13 3:27am    
where the declaration & declination of lbl[] and txt[] array..??

"lbl[i] (=lbl1)" for achieving this you have first initialize lbl[] array(Label Type) as
lbl[0] = lbl0; lbl[1] = lbl1; and so on....

As simple search shows, lbl is really never defined. So, do you think that it will be defined by a miracle? Make sure you define it.

—SA
 
Share this answer
 
Comments
Abdul Quader Mamun 23-Jan-13 3:35am    
It is not possible to define a number with textbox name.
Sergey Alexandrovich Kryukov 23-Jan-13 3:47am    
Why? And why are you even mention it? It all depends on what do you mean by "define".
—SA
Abdul Quader Mamun 23-Jan-13 4:18am    
It is not possible to define dynamic variable name or textbox name.
Sergey Alexandrovich Kryukov 23-Jan-13 4:28am    
OK, if you don't want to answer my question, don't...
—SA
try like follows

C#
cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("lbl{0}", i), false).First().Text);
         cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("txt{0}", i), false).First().Text);


first see the definition for Controls.Find
C#
public Control[] Find(
	string key,
	bool searchAllChildren
)

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find(v=vs.100).aspx[^]

suppose i=2
string.Format("lbl{0}", i) give the value =lbl2
this.Controls.Find search the controls by given key.

for ex:
after compiling the
this.Controls.Find(string.Format("lbl{0}", i), false).First().Text
it just like
this.Controls.Find("lbl2", false).First().Text
.First() is a LINQ to select the first element.

forget to mentioned you may have to include the namespace
using System.Linq;
 
Share this answer
 
v2
Comments
Elham.Deljooei 23-Jan-13 4:15am    
Excuse me. can i say me "cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("lbl{0}", i), false).First().Text);"
what does this do exactly? Because i Want variable value be part of textbox's name. for example i define lbl1 and in the code i want say lbl[i].Text. based on 'i' the code choose own lbl and send its text.
I have tried this but it isn't correct.
i'm sorry again. Thx for your help.
Tharaka MTR 23-Jan-13 4:33am    
What I understood is you have set of label and text boxes
for ex: lbl1, lbl2 lbl3,lbl4 etc ..
txt1,txt2,txt3 etc..
and you want to get the value of those controls dynamically. please correct me if I'm wrong.

If I am right, you can use this.Controls.Find() method for this.
Elham.Deljooei 23-Jan-13 4:44am    
Of course, you're right. but i don't know how to use this.
Exactly i want to say when variable 'i=1' then choose lbl1 and 'i=2' then choose lbl2(in a For).
Tharaka MTR 23-Jan-13 5:03am    
I have added the explanation by improving the solution. pls check
Elham.Deljooei 23-Jan-13 5:14am    
First i want to know Why 'false'?
i added Using System.Linq; but those lines don't run.When the bugger arrived this line it jump to Catch part.
Thn for your help.
You can do like the bellow.

C#
List<TextBox> textBoxList = new List<TextBox>();
textBoxList.Add(txtName);
textBoxList.Add(txtPhone);

TextBox txtName=textBoxList[1];
 
Share this answer
 
Comments
Elham.Deljooei 23-Jan-13 5:01am    
I don't know What you mean exactly?
I'm a new programer. I like Programing very much but i don't know how to start to learn c#.
can you instruct me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900