Skip to content
Advertisement

On button click append OR… in SQL Query

Is it possible to append to an SQL query based on number of clicks on a button?

Eg. For each time I click on the button (+) I want to add OR [PIN]=@LogID# Where # is a number increasing for everytime I press the plus (+) button.

I also would like do be able to decrease the same way by using the minus (-) button.

A new textfield should appear for every (+) click with the same number # as the appending line in the query OR [PIN]=@LogID# to my SQL query SelectCommand="SELECT * FROM [PINCode] WHERE ([PIN]=@LogID)". And decrease if I click on (-) button.

Any idea on how to do this? I would appreciate any links or suggestions pointing me in the right direction.

I´m not looking for a finished solution here just help on were to start. What to search for, where to read.

Implemented the example script recieved

Default.aspx.vb

Default.aspx

Advertisement

Answer

Sure, since you want to be able to add 1 to “N” choices?

Then that suggests a repeater solution.

So, say we have this markup:

So, we have a + button, a – button (for each row), a database search button, and of course our simple repeater.

So, the code to load this up, and say add a row, and delete a row, we get this code:

So, say we whack + a few times, and we thus have this:

enter image description here

So, each time we click the + key, we get a new row. We are free to type in a value.

So, our add button code is this:

And of course we need the – button, say like this:

Ok, so that gives us, “add as many as we want”

Or remove a row.

I do have that little routine that when we add or remove (or search), the user MIGHT have editing the values, so we have a routine to take the repeater rows, and send them back to the table. That little routine is this:

So, now the only “hard” part is the search routine.

We need N options.

We can NOT use sql concattion since this is USER input. If we were to generate the numbers and user could NOT edit/change, then ok, you could just concenate here.

So, we now need the “list” of choices and we STILL want strong typed parameters, nd we still want sql injection protection.

So, this will work:

so, not too much code. I think one could “try” just adding HTML for the add button but as the above shows? We have really un-limited flexibility, and you can add quite much plain jane markup to that repeater, and add more functionally over time – and not really have to change much code.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement