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("ConnectionString").ConnectionString)
conn.CommandText = "SELECT lastname From Profile WHERE ([UserId] = @UserId)"
conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString)
conn.CommandText = "SELECT LastName From Profile WHERE ([UserId] = @UserId)"
conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString)
conn.CommandText = "SELECT Address From Profile WHERE ([UserId] = @UserId)"
conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString)
conn.CommandText = "SELECT Photo From Profile WHERE ([UserId] = @UserId)"
conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString)
conn.CommandText = "SELECT Job From Profile WHERE ([UserId] = @UserId)"
conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString)
conn.CommandText = "SELECT Birthday From Profile WHERE ([UserId] = @UserId)"
conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString)
conn.CommandText = "SELECT Region From Profile WHERE ([UserId] = @UserId)"
conn.Parameters.AddWithValue("userid", 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
since you're hitting the same Database, with the same connection string, why not just grab the whole row, instead of just 1 column?
i tend to use Entity Framework instead of sql commmands
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 => x.UserId == userId).First(); // there should be only one
return person;
}
}
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
amr ragab
Member
57 Points
64 Posts
Possible any prefix to code
May 20, 2012 01:54 PM|LINK
Hi All
I Make Profile.vb Class
I Need any Possible prefix ( ShortCut ) to code
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("ConnectionString").ConnectionString) conn.CommandText = "SELECT lastname From Profile WHERE ([UserId] = @UserId)" conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString) conn.CommandText = "SELECT LastName From Profile WHERE ([UserId] = @UserId)" conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString) conn.CommandText = "SELECT Address From Profile WHERE ([UserId] = @UserId)" conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString) conn.CommandText = "SELECT Photo From Profile WHERE ([UserId] = @UserId)" conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString) conn.CommandText = "SELECT Job From Profile WHERE ([UserId] = @UserId)" conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString) conn.CommandText = "SELECT Birthday From Profile WHERE ([UserId] = @UserId)" conn.Parameters.AddWithValue("userid", 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("ConnectionString").ConnectionString) conn.CommandText = "SELECT Region From Profile WHERE ([UserId] = @UserId)" conn.Parameters.AddWithValue("userid", 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 Classrobwscott
Star
8079 Points
1491 Posts
Re: Possible any prefix to code
May 20, 2012 07:19 PM|LINK
since you're hitting the same Database, with the same connection string, why not just grab the whole row, instead of just 1 column?
i tend to use Entity Framework instead of sql commmands
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 => x.UserId == userId).First(); // there should be only one return person; } }then access your object as so
Mark Answered if it helps - Good luck!
Cheers!
Design And Align
- Rob
amr ragab
Member
57 Points
64 Posts
Re: Possible any prefix to code
May 21, 2012 07:48 AM|LINK
Thank you for help me
Beautiful idea, but I need to more explain because I used vb.net
robwscott
Star
8079 Points
1491 Posts
Re: Possible any prefix to code
May 21, 2012 12:36 PM|LINK
Mark Answered if it helps - Good luck!
Cheers!
Design And Align
- Rob