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;
}

0 comments:

Post a Comment