Showing posts with label userid. Show all posts
Showing posts with label userid. Show all posts

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

Pass UserID instead of Username to other pages after logged on, ASP.NET 2.0

Hi,
I have my own user table with definition like
UserID int not null primary key,
Username varchar(50) not null,
Password varchar(50) not null,
Firstname varchar(50) not null,
Lastname varchar(50) not null,
Email varchar(50) not null,
....
I create my own Membership provider to inherit SqlMembershipProvider
public class MyMembershipProvider :
System.Web.Security.SqlMembershipProvider {
public MyMembershipProvider() {
}
public override bool ValidateUser(string username, string password) {
// query my DB to verify user
MyUser mu = new MyUser();
return mu.VerifyUser(username, password);
}
}
In Asp.Net 1.1, we can use
FormsAuthentication.RedirectFromLoginPage(UserID.ToString(), false); to save
UserID (which is whatever I return from my own function, including UserID
from User table). Later on, we just call User.Identity.Name to retrieve the
UserID and it could be used as I like.
But in ASP.NET 2.0, I just need to add
<membership defaultProvider="MyMembershipProvider">
<providers>
<add name="MyMembershipProvider"
type="MyMembershipProvider"/>
</providers>
</membership>
to my web.config file, it will handle authentication autimatically. In this
case how can I pass UserID instead of username to other pages?
Thanks
HardyHardy,
Why pass it around? Why not set a session variable with the information
you need?
- Nicholas Paldino [.NET/C# MVP]
- mvp@.spam.guard.caspershouse.com
"Hardy Wang" <hardywang@.hotmail.com> wrote in message
news:umAPpLP%23FHA.1988@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I have my own user table with definition like
> UserID int not null primary key,
> Username varchar(50) not null,
> Password varchar(50) not null,
> Firstname varchar(50) not null,
> Lastname varchar(50) not null,
> Email varchar(50) not null,
> ....
> I create my own Membership provider to inherit SqlMembershipProvider
> public class MyMembershipProvider :
> System.Web.Security.SqlMembershipProvider {
> public MyMembershipProvider() {
> }
> public override bool ValidateUser(string username, string password) {
> // query my DB to verify user
> MyUser mu = new MyUser();
> return mu.VerifyUser(username, password);
> }
> }
> In Asp.Net 1.1, we can use
> FormsAuthentication.RedirectFromLoginPage(UserID.ToString(), false); to
> save UserID (which is whatever I return from my own function, including
> UserID from User table). Later on, we just call User.Identity.Name to
> retrieve the UserID and it could be used as I like.
> But in ASP.NET 2.0, I just need to add
> <membership defaultProvider="MyMembershipProvider">
> <providers>
> <add name="MyMembershipProvider"
> type="MyMembershipProvider"/>
> </providers>
> </membership>
> to my web.config file, it will handle authentication autimatically. In
> this case how can I pass UserID instead of username to other pages?
>
> Thanks
> Hardy
>

Friday, March 16, 2012

Passing an array from a function

I have a function that autenticates a user. Generally I just pass back true if the user is in the database, but now I want to pass back the userID and if the user is in the database. My guess is to do this with and array, but I keep having troubles passing it back. Code below.

Error I get is

Compiler Error Message:CS0029: Cannot implicitly convert type 'bool' to 'string[]'

Source Error:

Line 10: String strAuthenticate;Line 11:Line 12: userInfo = Authenticate(username.Text, password.Text);Line 13: strAuthenticate = userInfo[0];Line 14: if(strAuthenticate == "0"){

<code>

void login(Object s, EventArgs e){
String[] userInfo = new String[2];
String strAuthenticate;

userInfo = Authenticate(username.Text, password.Text);
strAuthenticate = userInfo[0];
if(strAuthenticate == "0"){
//creating a cookie to store user info
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
objTicket = new FormsAuthenticationTicket(1, username.Text, DateTime.Now, DateTime.Now.AddMinutes(240), false,"");
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);
Response.Cookies.Add(objCookie);

Response.Redirect("logedIn.aspx");
//FormsAuthentication.RedirectFromLoginPage(username.Text, false);
}
else{
lblLogin.Text = "INCORRECT USER NAME AND/OR PASSWORD";
}
}

bool Authenticate(string strUsername, string strPassword){
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["backupDataB"]);
OleDbCommand objCmd;
OleDbDataReader objDR;
bool userFound;
String userID;
String[] userInfo = new String[2];

objCmd = new OleDbCommand("SELECT * FROM users WHEREemail=@dotnet.itags.org.strUsername ANDPassword=@dotnet.itags.org.strPassword", objConn);
//get userID from this query and store it in a ticket
objCmd.Parameters.Add("@dotnet.itags.org.strUsername", strUsername);
objCmd.Parameters.Add("@dotnet.itags.org.strPassword", strPassword);

objConn.Open();
objDR = objCmd.ExecuteReader();
userFound = objDR.Read();
userID = objDR["userID"];
objDR.Close();
objConn.Close();

userInfo[0] = userFound;
userInfo[1] = userID;

return (String)userInfo;
}

</code>

You'll need to also change the definition of your function to return a string array:

bool Authenticate(string strUsername, string strPassword){

Marcie


I have changed what I think I need to change, but now I get this error and I'm not really sure why.

CS0165: Use of unassigned local variable 'userID'

void login(Object s, EventArgs e){
String[] userInfo = new String[2];
String strAuthenticate;

userInfo = Authenticate(username.Text, password.Text);
strAuthenticate = userInfo[0];
if(strAuthenticate == "0"){
//creating a cookie to store user info
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
objTicket = new FormsAuthenticationTicket(1, username.Text, DateTime.Now, DateTime.Now.AddMinutes(240), false, userInfo[1]);
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);
Response.Cookies.Add(objCookie);

Response.Redirect("logedIn.aspx");
}
else{
lblLogin.Text = "INCORRECT USER NAME AND/OR PASSWORD";
}
}

String[] Authenticate(string strUsername, string strPassword){
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["backupDataB"]);
OleDbCommand objCmd;
OleDbDataReader objDR;

String userFound = "no";
String userID;
String[] userInfo = new String[2];

objCmd = new OleDbCommand("SELECT * FROM users WHEREemail=@.strUsername ANDPassword=@.strPassword", objConn);
//get userID from this query and store it in a ticket
objCmd.Parameters.Add("@.strUsername", strUsername);
objCmd.Parameters.Add("@.strPassword", strPassword);

objConn.Open();
objDR = objCmd.ExecuteReader();
//"Read" reads each line that is returned from the datareader it starts one above the first record
//if the data is there it will return true if not it will return false. Here it is only examining to see
//if the datareader can read one line. If it can the record does exsit if it can't it means the reader
//returned nothing.
while(objDR.Read()){
userFound = "yes";
userID = objDR.GetString(0);
}
objDR.Close();
objConn.Close();

userInfo[0] = userFound;
userInfo[1] = userID;

return userInfo;
}


Your method signature specifies bool
bool Authenticate(string strUsername, string strPassword){

your return specifies a string
return (String)userInfo;

and userinfo is actually a string array

You need to make all these the same Type


That could happen if your query doesn't return any results (user is not found). You're only initializing userID in the while--read loop:

while(objDR.Read()){
userFound = "yes";
userID = objDR.GetString(0);
}

Marcie


Yes it was because I needed to intialize userID,

Thanks

void login(Object s, EventArgs e){
int[] userInfo = new int[2];
int strAuthenticate;

userInfo = Authenticate(username.Text, password.Text);
strAuthenticate = userInfo[0];
if(strAuthenticate == 1){
//creating a cookie to store user info
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
objTicket = new FormsAuthenticationTicket(1, username.Text, DateTime.Now, DateTime.Now.AddMinutes(240), false, userInfo[1].ToString());
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);
Response.Cookies.Add(objCookie);

Response.Redirect("logedIn.aspx");
}
else{
lblLogin.Text = "INCORRECT USER NAME AND/OR PASSWORD";
}
}

int[] Authenticate(string strUsername, string strPassword){
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["backupDataB"]);
OleDbCommand objCmd;
OleDbDataReader objDR;

int userFound = 0;
int userID = 0;
int[] userInfo = new int[2];

objCmd = new OleDbCommand("SELECT * FROM users WHEREemail=@.strUsername ANDPassword=@.strPassword", objConn);
//get userID from this query and store it in a ticket
objCmd.Parameters.Add("@.strUsername", strUsername);
objCmd.Parameters.Add("@.strPassword", strPassword);

objConn.Open();
objDR = objCmd.ExecuteReader();
//"Read" reads each line that is returned from the datareader it starts one above the first record
//if the data is there it will return true if not it will return false. Here it is only examining to see
//if the datareader can read one line. If it can the record does exsit if it can't it means the reader
//returned nothing.
while(objDR.Read()){
userFound = 1;
userID = objDR.GetInt32(0);
}
objDR.Close();
objConn.Close();

userInfo[0] = userFound;
userInfo[1] = userID;

return userInfo;
}