p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   How to update a label controll from a db (http://p2p.wrox.com/showthread.php?t=71945)

bex December 23rd, 2008 01:03 PM

How to update a label controll from a db
 
Hi there i need to pull data from a database for each individual user account and show it to a label controll or any controllon in which the loged user can see its posts which will varey from user to user, something similar to the wrox forums, each registred user has a diferent number of posts/replays last visit etc.only that has to be visible to the user only


any help apprisiated

alliancejhall December 23rd, 2008 01:12 PM

What part...
 
what part do you need help with? connecting to the database? retrieving the data? assigning the data to the label? We need more info, like what database are you using and what part specifically do you need help with? what do you have so far?

bex December 23rd, 2008 01:36 PM

I have a sql2005 DB the tabel has an ID ,Name, Surname and Contact column.
In the Contact column is stored the user name, i need too count the usernames in that column and show them as int for each loged user.

exampel: my user name is bex and in the contact Column bex exists lets say 5 times now the label in default.aspx should display 5;

1)i need help with retrieving the data
2)may be conversion to int..


thanks for you quick replay

alliancejhall December 23rd, 2008 01:50 PM

So...
 
you need help with the query to find the count?


Select Count(UserName) As ContactCount From tblUsers Where UserName=@UserName

this here is searching your database for users with the username supplied by you (@UserName is the search parameter) and counting them all up and returning that count

does that help you with what you need or do you also need the code to connect to the db run this query and return that data? if so what language?
C#? VB.net?

bex December 23rd, 2008 01:58 PM

VB plase
i need to call that on page load

alliancejhall December 23rd, 2008 02:28 PM

I would..
 
I would create a function that get's the users count then call that function on the page load. I'm going to assume you are storing the username in a session variable and assume your table names as well. This code is just an example of what to do you would need to adapt it to suite your needs.

Code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      If Not Page.IsPostBack Then
          Dim Count As String = GetUserCount(Session("UserName").ToString).ToString

          If Not String.IsNullOrEmpty(Count) Then
              lblUserCount.Text = Count
          Else
              lblUserCount.Text = "Sorry could not retrieve count."
          End If

        End If

End Sub

In the above code i am calling the function GetUserCount and passing the username which i assume is stored in a session variable and assigning it's return value to a variable named Count. I then check if that variable has a value. If it does then I assign the label lblUserCount text the count. If there is no value for count I tell the user with the text of the label.


Below is the actual GetUserCount function. First I create my connection to the DB then my query and my reader. I then create a string variable and assign it the UserName value. I then check to make sure it has a value if not i dispose of all the variables then return nothing and exit the function.
otherwise i open my connection run the query check to make sure that i return rows then if there are rows i return the result of the query. then close my connection and reader and dispose my variables.
Code:

Private Function GetUserCount(ByVal UserName As String) As String
       
        Dim sqlCon As New System.Data.SqlClient.SqlConnection("Connection String Here")
        Dim sqlCmd As New System.Data.SqlClient.SqlCommand("Select Count(UserName) As UserCount From tblUsers Where UserName=@UserName")
        Dim sqlDR As System.Data.SqlClient.SqlDataReader = Nothing

        Dim strUserName As String = Nothing

        Try
            strUserName = UserName
        Catch
            strUserName = Nothing
            Count = Nothing
        End Try

        If String.IsNullOrEmpty(strUserName) Then
            sqlCon.Dispose()
            sqlCon = Nothing
            sqlCmd.Dispose()
            sqlCmd = Nothing
            sqlDR = Nothing
            strUserName = Nothing
            Count = Nothing
            Return Nothing
            Exit Function
        End If

        Try
            sqlCon.Open()
            sqlCmd.CommandType = CommandType.Text
            sqlCmd.Parameters.AddWithValue("@UserName", strUserName)
            sqlCmd.Connection = sqlCon
            sqlDR = sqlCmd.ExecuteReader
            sqlDR.Read()

            If sqlDR.HasRows Then
              Return sqlDR("UserCount").ToString
            End If

        Finally
            sqlDR.Close()
            sqlDR = Nothing
            sqlCmd.Dispose()
            sqlCmd = Nothing
            sqlCon.Close()
            sqlCon.Dispose()
            sqlCon = Nothing
            strUserName = Nothing
        End Try
End Function

Like I said this is an EXAMPLE you would have to adapt it to suite your structure and names. But this should give you a pretty good Idea. There may be some overkill in there but i like to to be safe rather than sorry.

bex December 23rd, 2008 03:01 PM

Thanks that is what i was looking for.
can i ask you somethig about aspnet database? if i can do you know how wto delete a user from ASPNet DB.
thanks agen for the code

alliancejhall December 23rd, 2008 03:09 PM

Sorry...
 
But I never use that database... perhaps you could start a new thread and someone else can help.

gbianchi December 23rd, 2008 07:36 PM

Hi there.. What do you mean?? you want to delete a user from the user management database? again, you want to do it in code, or just in the database??

bex December 24th, 2008 06:35 AM

I just want to delete the user from the db so it can not log in next time the user attempts to log in.


All times are GMT -4. The time now is 11:03 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.