Link to home
Start Free TrialLog in
Avatar of sbornstein2
sbornstein2

asked on

Replacing Stored Proc Parameter each time

Hi all I have a stored proc and code that sets parameters etc.  I need to use the same insert 10 times and replace two parameters each time.  How do I do that?  For example,

command.Parameters.Add("@MYID", SqlDbType.Int).Value = 25;
command.Parameters.Add("@MYHRS", SqlDbType.Int).Value = 6;

then I run it.   Next lines I need to replace the above values?

command.Parameters.Add("@MYID", SqlDbType.Int).Value = 87;
command.Parameters.Add("@MYHRS", SqlDbType.Int).Value = 43;
ASKER CERTIFIED SOLUTION
Avatar of jefftan
jefftan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sbornstein2
sbornstein2

ASKER

ya what do you think about that actually I was going to do this the hard way.   I have 10 textboxes with numbers in it for each "phase" of hours.   I need to unfortunatly because the database design is so bad, I did not create it :).  I need to insert 10 records in this table.  I also need to pass in a hardcoded ID because of a 3 part key.   So iterating through the textboxes is probably doable but for each of the 10 textboxes I need to pass along an ID that will go into the @MYID parameter hardcoded.   Do you know of a better way I might be able to approach this to insert the 13 records.  The other thing I was thinking was to pass all 10 boxes into the proc but then I will need to change it in both places DB and code behind on an added change etc.   Thanks so much jeff
can u give me more description on what is ur value (@MYID, @MYHRS)is taken from? hardcoded ID for each 10 textbox(user input) ? 13 records? Sorry, i am not getting the picture clear enough... maybe i need to wear thicker glasses (getting old...) .. :)
no not at all.  thanks.  What I have is 10 textboxes that are setup like so:

txtGrp2Box1, txtGrp2Box2, txtGrp2Box3......txtGrp2Box10

so I can probably iterate through that 10 times with a for loop or something and insert the records for 10 times.  But then I have to pass in a hardcoded ID the @MYID.  Maybe I can use some type of array where I can setup the ID's for each insert.  For example,

txtGrp2Box1.Text = 10 for @MyHrs
txtGrp2Box2.Text = 30 for @MyHrs


But the @MYID I need to pass in for each hardcoded such as 20 might be the ID for txtGrp2Box1 I need to pass with it.  Problem is we have this combination table that stores an ID for a combination of 3 fields.  Well it is a real pain to have to go dig through and pass all the 3 parameters for the combination so I want to pass the ID field number instead.  But it is an identity column I need to look up.
ok. If i'm getting it right, u want to have 10 textbox on ur form, each textbox will be assigned a unique harcoded ID which will be the value of @MYID.
Then the textbox will be receiving input from user which is the value of @MYHRS.
Am i rigth?
Assumming i'm right...

instantiate an array of textbox, and Id (which store the hardcoded Id).

private const int COUNT = 10;
private TextBox[] txtInput = new TextBox[COUNT];
private int[] ID = new int[COUNT];

assign the ID array with ur hardcoded ID (Or u lookup in db, maybe).

put the textbox to the form. (arrange the coordinate urself...)

for (i = 0; i < TEXTBOX_COUNT; ++i)
{
      txtInput[i] = new TextBox();
      txtInput[i].Bounds = new Rectangle(20, 20 + (20 * i), 100, 100);
      txtInput[i].Parent = this;
}



Then when u use the procedure call, u may do like this...

for (int i = 0; i < COUNT; ++i)
{
    command.Parameters["@MYID"].Value = ID[i];
    command.Parameters.["@MYHRS"].Value = Convert.ToInt32(txtInput[i].Text);
    command.ExecuteNonQuery();
}

hope it helps...
you got it :)
I will try that thanks so much Jeff :)