Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    .Net Coding. (Changing the text)

    Hi all,

    I was just wondering how can I code this way..

    For example I had a button and a textbox and when I click on it... it will connect the database and change the label.text to the information I wan.. which is "either 1 or 0".

    Like I have a few user name - "Tom, Dick and Harry"
    I enter tom in the textbox and I hit the button. it will load the database and show on the label (Or anything you want).... how many books he rent. actually is either 1 or 0.


    I can get it done by using the Gripview control... but I was thinking if there is anyway I can get it done without using it? how can I do a database connection and get information I want without using the gripview data?

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    as you didn't provide any code...I'll be making some assumptions here to show how you can get this done in asp.net using c#

    Let's say you have a deafult.aspx page and it contains the following code within the form's tag

    Code:
    <asp:Label ID="Label1" runat="server" Text="Number of books will be shown here">
    <br />
    <asp:Button ID="btnShowBooks" runat="server" Text="Show Books" onclick="btnShowBooks_Click" />
    Here is the code in the button's click event

    PHP Code:
    protected void btnShowBooks_Click(object senderEventArgs e)
    {

    /* get the connection string from the web.config file
       if you haven't added one already, here's an example
     
     <connectionStrings>
        <add name="MyConnectionString" 
            connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SiteDb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
    </connectionStrings>
     
    Please visit this site for connection string examples http://www.connectionstrings.com/

    */
    string dbConnectionString System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

    using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection())
    {
        
    connection.ConnectionString dbConnectionString;
        
    connection.Open();

        
    System.Data.SqlClient.SqlCommand cmd connection.CreateCommand();
        
        
    // assuming you have a table in your db defined as
        // CREATE TABLE [dbo].[Users] ( UserID INT IDENTITY(1,1), Name VARCHAR( 50 ), Books Bit )

        
    cmd.CommandText "SELECT [Books] FROM dbo.Users WHERE [UserId] = '1'";

        
    System.Data.SqlClient.SqlDataReader reader cmd.ExecuteReader();

        if (
    reader.HasRows)
        {
            
    // as we only have one record in the db, we don't need a while 
            // loop to iterate over the the records in the reader object
            
    reader.Read();

            
    // assign the value read from the database to the label
            
    Label1.Text reader["Books"].ToString();

        } 
    //-- ends if
       

    //-- ends using
    //-- ends btnShowBooks_Click 
    Hope this helps

    Cheers
    ~E

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First you have to create tables for user id and user name. Now when you start to run the query fetch the username according to userid and store that result in particular variable then display the result on particular label.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •