Possible any prefix to codehttp://forums.asp.net/t/1805533.aspx/1?Possible+any+prefix+to+codeMon, 21 May 2012 12:36:09 -040018055334988720http://forums.asp.net/p/1805533/4988720.aspx/1?Possible+any+prefix+to+codePossible any prefix to code <p>Hi All</p> <p>I Make Profile.vb Class</p> <p>I Need any Possible prefix ( ShortCut ) to code</p> <pre class="prettyprint">Imports Microsoft.VisualBasic Imports System.Data.SqlClient Public Class UserProfileclass Public Function name() As String Using conn As New SqlCommand conn.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;ConnectionString&quot;).ConnectionString) conn.CommandText = &quot;SELECT lastname From Profile WHERE ([UserId] = @UserId)&quot; conn.Parameters.AddWithValue(&quot;userid&quot;, Membership.GetUser.ProviderUserKey.ToString) conn.Connection.Open() Dim reader As SqlDataReader = conn.ExecuteReader Do While reader.Read name = reader.GetString(0) Loop conn.Connection.Close() End Using Return name End Function Public Function LastName() As String Using conn As New SqlCommand conn.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;ConnectionString&quot;).ConnectionString) conn.CommandText = &quot;SELECT LastName From Profile WHERE ([UserId] = @UserId)&quot; conn.Parameters.AddWithValue(&quot;userid&quot;, Membership.GetUser.ProviderUserKey.ToString) conn.Connection.Open() Dim reader As SqlDataReader = conn.ExecuteReader Do While reader.Read LastName = reader.GetString(0) Loop conn.Connection.Close() End Using Return LastName End Function Public Function Address() As String Using conn As New SqlCommand conn.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;ConnectionString&quot;).ConnectionString) conn.CommandText = &quot;SELECT Address From Profile WHERE ([UserId] = @UserId)&quot; conn.Parameters.AddWithValue(&quot;userid&quot;, Membership.GetUser.ProviderUserKey.ToString) conn.Connection.Open() Dim reader As SqlDataReader = conn.ExecuteReader Do While reader.Read Address = reader.GetString(0) Loop conn.Connection.Close() End Using Return Address() End Function Public Function Photo() As String Using conn As New SqlCommand conn.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;ConnectionString&quot;).ConnectionString) conn.CommandText = &quot;SELECT Photo From Profile WHERE ([UserId] = @UserId)&quot; conn.Parameters.AddWithValue(&quot;userid&quot;, Membership.GetUser.ProviderUserKey.ToString) conn.Connection.Open() Dim reader As SqlDataReader = conn.ExecuteReader Do While reader.Read Photo = reader.GetString(0) Loop conn.Connection.Close() End Using Return Photo End Function Public Function Job() As String Using conn As New SqlCommand conn.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;ConnectionString&quot;).ConnectionString) conn.CommandText = &quot;SELECT Job From Profile WHERE ([UserId] = @UserId)&quot; conn.Parameters.AddWithValue(&quot;userid&quot;, Membership.GetUser.ProviderUserKey.ToString) conn.Connection.Open() Dim reader As SqlDataReader = conn.ExecuteReader Do While reader.Read Job = reader.GetString(0) Loop conn.Connection.Close() End Using Return Job End Function Public Function Birthday() As String Using conn As New SqlCommand conn.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;ConnectionString&quot;).ConnectionString) conn.CommandText = &quot;SELECT Birthday From Profile WHERE ([UserId] = @UserId)&quot; conn.Parameters.AddWithValue(&quot;userid&quot;, Membership.GetUser.ProviderUserKey.ToString) conn.Connection.Open() Dim reader As SqlDataReader = conn.ExecuteReader Do While reader.Read Birthday = reader.GetString(0) Loop conn.Connection.Close() End Using Return Birthday End Function Public Function Region() As String Using conn As New SqlCommand conn.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;ConnectionString&quot;).ConnectionString) conn.CommandText = &quot;SELECT Region From Profile WHERE ([UserId] = @UserId)&quot; conn.Parameters.AddWithValue(&quot;userid&quot;, Membership.GetUser.ProviderUserKey.ToString) conn.Connection.Open() Dim reader As SqlDataReader = conn.ExecuteReader Do While reader.Read Region = reader.GetString(0) Loop conn.Connection.Close() End Using Return Region End Function End Class</pre> <p><br> <br> </p> 2012-05-20T13:54:15-04:004988893http://forums.asp.net/p/1805533/4988893.aspx/1?Re+Possible+any+prefix+to+codeRe: Possible any prefix to code <p>since you're hitting the same Database, with the same connection string, why not just grab the whole row, instead of just 1 column?</p> <p>i tend to use Entity Framework instead of sql commmands</p> <pre class="prettyprint">public Person GetPerson() { using (YourContext context = new YourContext()) { int userId = int.Parse(Membership.GetUser.ProviderUserKey.ToString()); // userid should be an int?? Person person = context.Person.Where(x =&gt; x.UserId == userId).First(); // there should be only one return person; } }</pre> <p>then access your object as so</p> <pre class="prettyprint">this.LabelFirstName.Text = person.FirstName; this.LabelLastName.Text = person.LastName;</pre> 2012-05-20T19:19:44-04:004989508http://forums.asp.net/p/1805533/4989508.aspx/1?Re+Possible+any+prefix+to+codeRe: Possible any prefix to code <p><span class="hps">Thank you for</span><span>&nbsp;</span><span class="hps">help</span><span>&nbsp;</span><span class="hps">me</span><br> <span></span><span class="hps">Beautiful</span><span>&nbsp;</span><span class="hps">idea</span><span>, but I</span><span>&nbsp;</span><span class="hps">need</span><span>&nbsp;</span><span class="hps">to more&nbsp;explain</span><span>&nbsp;</span><span>&nbsp;</span><span class="hps">because I</span><span>&nbsp;</span><span class="hps">used</span><span>&nbsp;</span><span class="hps">vb.net</span></p> 2012-05-21T07:48:59-04:004990248http://forums.asp.net/p/1805533/4990248.aspx/1?Re+Possible+any+prefix+to+codeRe: Possible any prefix to code <pre class="prettyprint">Public Function GetPerson() As Person Using context As New YourContext() Dim userId As Integer = Integer.Parse(Membership.GetUser.ProviderUserKey.ToString()) ' userid should be an int?? Dim person As Person = context.Person.Where(Function(x) x.UserId = userId).First() ' there should be only one Return person End Using End Function</pre> <pre class="prettyprint">Me.LabelFirstName.Text = person.FirstName Me.LabelLastName.Text = person.LastName</pre> <pre id="code-result" class="brush:vbnet"></pre> <pre id="code-result" class="brush:vbnet"></pre> 2012-05-21T12:36:09-04:00