Not getting return value
Posted
by
scottO
on Stack Overflow
See other posts from Stack Overflow
or by scottO
Published on 2012-12-09T23:00:26Z
Indexed on
2012/12/09
23:04 UTC
Read the original article
Hit count: 147
I am trying to get a return value and it keeps giving me an error. I am trying to grab the "roleid" after the username has been validated by sending it the username-- I can't figure out what I am doing wrong?
public string ValidateRole(string sUsername)
{
string matchstring = "SELECT roleid FROM tblUserRoles WHERE UserName='" + sUsername +"'";
SqlCommand cmd = new SqlCommand(matchstring);
cmd.Connection = new SqlConnection("Data Source=(local);Initial Catalog="mydatabase";Integrated Security=True");
cmd.Connection.Open();
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter();
DataTable dt = new DataTable();
sda.SelectCommand = cmd;
sda.Fill(dt);
string match;
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
match = row["roleid"].ToString();
return match;
}
}
else
{
match = "fail";
return match;
}
}
© Stack Overflow or respective owner