i created a class called ProfileHelper, and I can’t seem to get my get/set accessors correct; instead, I’m getting red lines on both get and set. Here is the code I am trying to use:
public static String UserName(string columnName)
{
get
{
using (SqlConnection cn = new SqlConnection(SiteConfig.ConnectionString))
{
string sSql = ("SELECT UserName , LoweredUserName FROM aspnet_Users WHERE UserId = @UserId");
using (SqlCommand cm = new SqlCommand(sSql, cn))
{
cm.Parameters.AddWithValue("@UserId", Membership.GetUser().ProviderUserKey.ToString());
cn.Open();
using (SqlDataReader rd = cm.ExecuteReader())
{
while (rd.Read())
{
return columnName;
}
rd.Close();
}
cn.Close();
}
}
return columnName;
}
set
{
using (SqlConnection cn = new SqlConnection(SiteConfig.ConnectionString))
{
string sSql = ("UPDATE [aspnet_ Users] SET UserName = @UserName, LoweredUserName = @LoweredUserName WHERE UserId = @UserId");
using (SqlCommand cm = new SqlCommand(sSql, cn))
{
cm.Parameters.AddWithValue("@UserId", Membership.GetUser ().ProviderUserKey.ToString());
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
}
}
}
}
Syntax is basically:
Alternatively, auto-property do the backing field for you:
You should not fiddle around with SQL connections in properties. Access to properties should be fast and reliable. Consider replacing it by methods to make it clear that this is actually a longer-enduring action with external dependencies (which is more likely to fail):