0

I'm pretty new to c# and I'm struggling with this,

I'm trying to make a program that can send "items" to the names I type in the textboxes all at once with SQL stored produce.

I want to while loop textboxes so it changes from textBox1 to textBox2 and so on when it loops. I want to do this cause I have 45 texBoxes and a loop should be good I guess.

this is what I've tried:

int x = 1;
while (x < 46)
{
    cmd.CommandText = "EXECUTE usp_GiftItem @sendUser, @SendNickname, @receiver, @itemname, @text, @paymentType, @bOutput";
    cmd.Parameters.Clear();
    cmd.Parameters.Add("@SendUser", SqlDbType.VarChar).Value = "123";
    cmd.Parameters.Add("@SendNickname", SqlDbType.VarChar, 50).Value = textBox89.Text;
    cmd.Parameters.Add("@receiver", SqlDbType.VarChar, 50).Value = textbox(x).Text;
    cmd.Parameters.Add("@itemname", SqlDbType.VarChar).Value = textBox80.Text;
    cmd.Parameters.Add("@text", SqlDbType.VarChar, 1024).Value = textBox88.Text;
    cmd.Parameters.Add("@paymentType", SqlDbType.VarChar).Value = "0";
    cmd.Parameters.Add("@bOutput", SqlDbType.VarChar).Value = "1";
    cn.Open();
    cmd.ExecuteNonQuery();
    MessageBox.Show("Item sent", "Success");
    cn.Close();
    x++;
}

The problem is at @recivers value

I hope you there is a solution to this. Thank you, Much appreciated

2
  • 2
    Any comments for the downvotes? Commented Oct 10, 2013 at 14:36
  • Although I don't have an issue with how you're using the while loop, a for loop would be a better choice. You can create x, define the number of loops, and increment in one line. for (int x = 1, x < 46, x++) { ... Commented Oct 10, 2013 at 14:39

2 Answers 2

4
TextBox[] textBoxes = new TextBox[] { textBox1, textBox2, ... };
while(x ...)
{
    something[x] = textBoxes[x].Text;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! it's working. :] I wish I could vote up but my reputation is only 1 :s
1

You can simplify things if you do some preparation first. To example, you can create an array of items, where index of item is a TextBox number and item itself is an object what will be used to generate bold part: cmd.Parameters.Add( "@blablabla", type, num ).Value .

So:

 public class MyData
 {
     public string Text;
     public SqlDbType Type;
     public int Number;

     public MyData(string text, SqlDbType type, int number = 0)
     {
         Text = text;
         Type = type;
         Number = number;
     }
 }

 MyData[] data = new MyData[45] { new MyData("@bOutput", SqlDbType.VarChar), new MyData(...), ... };
 TextBox[] control = new TextBox[45] {textBox1, textBox2, ... };

 for(int i = 0; i < 45; i++)
     cmd.Parameters.Add(data[i].Text, data[i].Type, data[i].Number).Value = control[i].Text;

There are still things to improve, to example, instead of enumerating TextBox'es you may want to find them on the form (form1.Controls). Instead of generating data[] - it can be a part of TextBox.Tag, where you parse it and then generate parameters. Processing of the Number can be optional, etc..

But this should give you an idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.