Monday, March 26, 2012

Pass UserID in hidden field

I am trying to pass the UserID from a session variable into a new record.
I have a hidden textbox with the userID session in it, but the UserID field is int,4
and the textbox only pases text (I think). So I get an input string error.
am I doing this right?
All help apreciated.
Thanks,
JBIf you've got in in session, you shouldn't need to pass it in from the hidden field.

Either way:

Convert.ToInt32(Session.Item("UserID"))

or

Convert.ToInt32(hiddedTextBox.Text)

Regards,

Xander
Thanks Xanderno,
I've been at this all afternoon with no luck.
I get "input string was not in a correct format"

The parameter code is

myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))
myCommand.Parameters("@.MemberID").Value = Convert.ToInt32(UserID)

and the hidden text box is

<asp:TextBox id="UserID" runat="server" text='<% session(UserID")%>' Visible="False"
The database types are correct - any ideas?

Much appreciated,
JB
That means (likely) that UserID is blank. Which, (Ah ha!) looking at your code, it would be.

First, he session variable needs to be written to the html in order to appear in textbox or hidden field.
You could do this with either the full Response.Write: Response.Write(Session.Item("UserID"))
Or with the shorthand equals sign: =Session.Item("UserID")

Beyond that, since you're using an ASP.Net TextBox, with the visibility property set to false,
even if the session variable *was* being response.written, it still wouldn't work, because that textbox wouldn't be sent to the browser!

So, let's try something like this instead:

<input id="UserID" type="hidden" value="<% =Session.Item("UserID") %>" /
And that should fix you up.

Xander
Thanks Xander,

I'm getting closer but it now passes a "0" to the DB
If I use 'input' with an id of UserName instead of a textbox then in code behind it does not get declared.
So I declare it by

Dim UserID as Int32

Does this mean I have already converted UserID and so do not have to use Convert.Toint32 (UserID.text)

but then in my parameters

myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))
myCommand.Parameters("@.MemberID").Value = UserID.what goes here? I only get a few choices and was hoping for 'value'

I'm sure this is wrong!
Thanks again.
JB
This is a totaly wrong assumption you make there..

Look at:

http://www.asp.net/Tutorials/quickstart.aspx

And find yourself a few samples to play with. It will help you to get the ideas one-by-one..

For the code above:


Dim UserID as Int32
//set the user id to 1
UserID = 1

myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))

//set my param to the value of the userid var.
myCommand.Parameters("@.MemberID").Value = UserID


Actually, you're pretty close already.

First off let's change the tag a bit.

<input name="UserID" type="hidden" value="<% =Session.Item("UserID") %>" /
Now, when you pull up the page, if you do a View | Source, you should see that tag on your page, with your UserID set as the value. If it doesn't have a value, then there is something wrong with your session variable that you need to hunt down.

Next, in you're code, you'll have this:

Dim UserID as Int32
'Now we have the variable, but we still have to assign it a value.

UserID = Convert.ToInt32(Request.Form("UserID"))
'Now that we have the UserID in the variable, pass it to the command object.

myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))
myCommand.Parameters("@.MemberID").Value = UserID
Thanks,

I have looked through all the quickstarts but canot find this.
All my other parameters pass properly except this one
The value must equal the hidden input session variable from the form but it just passes a "0"

Thanks for the help so far.
JB
Xanderno

That's fixed it!!

Thanks to you and the others for taking time to help me.
Cheers,
JB

0 comments:

Post a Comment