I have 10 Lable and 10 TextBox.
I want to send a number of contorols on the form to sql and sql to do own order base on this number.
How to determine each number of those contorols?
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();
}
}
}
}
}
elham deljooeiPosted Jan 25, 2013, 12:38 PM
i put i=1.
Thx for your answer.
VulpesPosted Jan 25, 2013, 5:43 AM
When i = 0, that line is looking for a label named lbl0 which doesn't exist as your numbering starts at lbl1. Consequently, the First() extension method throws an exception because there are no elements in the sequence.
To correct it, change the line to:
cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("lbl{0}", i + 1), false).First().Text);
elham deljooeiPosted Jan 25, 2013, 1:44 AM
I put this line
cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("lbl{0}", i), false).First().Text);
its eeror is "Sequence contains no elements".
rather than
cmd.Parameters.AddWithValue("@clrke_Percent", lbl[i].Text);// this line it isn't correct.
VulpesPosted Jan 23, 2013, 10:17 AM
Can you pinpoint on which line the error is occurring?
elham deljooeiPosted Jan 23, 2013, 6:35 AM
{
try
{
if (lbl1.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", "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("@epcnt_Name", "?????");
//cmd.Parameters.AddWithValue("@clrke_Percent", lbl1MadrakeTahsili[i].Name.su
//cmd.Parameters.AddWithValue("@clrke_Tozihat", txtRaftar[i].);
cmd.Parameters.AddWithValue("@i", i);
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);
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
catch
{
MessageBox.Show(".Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I have som TextBox and Lable when their names are txt1,txt2,txt3... and lbl1,lbl2,lbl3,...
I want variable value be part of Textbox's name. For example i define lbl1 in Form for a Lable's name and in the code i want to say lbl[i].Text. Base on'i' the code choose own lbl and send its text. Insted number i want to put variable
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.
VulpesPosted Jan 23, 2013, 5:46 AM
Can you post the actual code which is giving you that error, please.
elham deljooeiPosted Jan 23, 2013, 1:35 AM
When i write this line, I get this eeror
"Error2The name 'lbl' does not exist in the current contextD:\Project\evaluation\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs976119WindowsFormsApplication1"
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.
Thx for your help.
VulpesPosted Jan 22, 2013, 9:30 AM
If so, then how you send it depends on what the stored procedure is expecting so that it can distinguish the number from the text.
For example, if it's expecting both to be sent as a single parameter with the number coming first followed by a space and then the text, you could do:
elham deljooeiPosted Jan 22, 2013, 6:46 AM
VulpesPosted Jan 22, 2013, 6:23 AM