Help me on this with SQLPARAMETERS

P

Patrick.O.Ige

I have a parameter below and i'm passing the value via Store procedure
Cmd.Parameters.Add(New SqlParameter("@ProductID", SqlDbType.Int, 1)).Value =
104
But as you can see the value "104" is hard coded..
My problem is on the same page i have a CHECKBOXLIST which is DAtabinded
and the DataValuefield i binded it to increments 1,2,3... which is p_id(I'm
doing that becos i need to pass those values)
My table looks like this :-
p_id ProductId chkText
1 104 Bank
2 104 Phone

My problem is i want to retrun the value 104 and not hard code it
there.Would it possibe to query the table like using:-
select TOP 1 productid from ProductFeatures which just returns value "104"
and append it to the Sqlparameters above.
any ideas?
 
G

Grant Merwitz

Why not user a DropDownList, or a RadioButton list to stored your Product
ID's.

The user can select that and you can build your Parameter based on that

so:

DropDownList (this would be loaded dynamicly setting the value to your
ProductID)

<asp:DropDownList id="dd" runat="server">
<asp:ListItem value="104">Bank</asp:ListItem>
<asp:ListItem value="104">Phone</asp:ListItem>
</asp:DropDownList>

Parameter:

Cmd.Parameters.Add(New SqlParameter("@ProductID", SqlDbType.Int,
1)).Value = dd.SelectedValue;


HTH
 
P

Patrick Olurotimi Ige

Grant thanks for the idea..
But i don't want it this way:-
<asp:DropDownList id="dd" runat="server">
<asp:ListItem value="104">Bank</asp:ListItem>
<asp:ListItem value="104">Phone</asp:ListItem>
</asp:DropDownList>

I want it dynamic so i have Databinded a DropDownlist.
But something seems funny here if i use
tha above it works

and if i don't databind it works
But if i databind it and then set :- Cmd.Parameters.Add(New
SqlParameter("@ProductID", SqlDbType.Int,
1)).Value = dd.SelectedValue;
it seems not to read the DropDwon selected value!!!

But i notice i can get the Selected value on page_load
And again if i view the source i can see the values are set well..

I have also set visible to FALSE since i don't want to make the
DropDownList visiblae but just to use it to set the value..

in page_load i have
If Not Page.IsPostBack Then

DropDown_Features()'dropdownlist
GetValue()'dropdownlist where i get the
dd.SelectedItem.Value
'Response.Write("" & dd.SelectedItem.Value & "")
End If

But in my sub DropDown_Features()if i try and set
Cmd.Parameters.Add(New SqlParameter("@ProductID", SqlDbType.Int,
1)).Value = dd.SelectedValue;

I get an error "Input string was not in a correct format.
"
Any ideas?
 
G

Grant Merwitz

Ok, i'm officially confused.

Why would you want to set the DropDownList's visibility to FALSE.
If its not something the user is selecting, then it should be something that
worked out in the code.
Either stored when the page loads, and called when you need it.
Or, worked out when you need it based on whatever criteria you've used.

Is this a case where it will alway be one value (i don't mean like 104, but
both bank and phone have 104)
So is it just a Product ID that can be stored as one value for the given
page.

Like:
A page that displays a product that a user can update and you are trying
to store the product id to do the update.


What i'm getting at, is if the dropdownlist is not visible, the user is not
selecting a value from it, so how do you know which item from that list to
use.
Or better yet, if you do know which item to use, why do you need a
dropdownlist of options, when you infact only need one option?
 
P

Patrick Olurotimi Ige

Grant..
Thats very right that if the user doesn't select a DropDwonList i can't
get the selected value but if i set :-
dd.SelectedItem.Value of the DropDownList in Page_Load i can recieve the
value. But seeting it beyond page_load won't work..
Any other ideas how i can store the value and then use it later on?
 
G

Grant Merwitz

Yes!

either Session or ViewState it.

So when you page loads, and your have a value called MyValue of type Int;
In the Page_Load do: (in c#)
Session["MyValue"] = MyValue;
or
ViewState["MyValue"] = MyValue;

Then when you want to retrieve it:
MyValue = (int)Session["MyValue"];
or
MyValue = (int)ViewState["MyValue"];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top