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 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Page has no output

    Hi! I need help about this one:
    What I should do:
    -I need to get the last login date of the user.

    Problem:
    The program does not give any date or error that's why I don't know what really happen.

    sample code:

    if (Session["logon"] != null){
    if ((sqlConn.State == ConnectionState.Closed))
    {
    sqlConn.Open();
    }
    string logs = string.Empty;
    string strCn = string.Empty;
    string _dt = string.Empty;
    string ndt = _dt;
    DataSet ds1 = new DataSet("logs");
    int badge = mylib.getBadgeNo(Page.User.Identity.Name.Remove(0, 9).ToString());
    strCn = "select max(dt) as logs from logs where usr_badge_no='"+ badge +"'";
    mylib.InitDataset(strCn, ds1);
    //if (ds1.Tables[0].Rows.Count != 0){

    _dt = ds1.Tables[0].Rows[0]["logs"].ToString();
    //}
    _dt = ndt;
    lbllstlogin.Text = ndt; //this is a label where the last login date should be place
    }

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Hey newbb,

    I'm not entirely sure what's happening inside your code there, but I can speculate(or guess) that mylib.InitDataset(...) runs the given SQL query on some DB and puts the data returned into DataTable ds1.Tables[0]? Something like that? If so, that seems kind of a strange way of doing that. If you're going that route, at least have the InitDataset method return either a DataTable or the actual String value which you're later trying String _dt to. Maybe that's the problem?

    In any case, surround your code with a try..catch. Inside the catch, do a Response.Write on the exception message (http://msdn.microsoft.com/en-us/libr...n.message.aspx)

    But even before you do that, run the SQL manually on the DB and see what you get.

    Mike

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 2 Times in 2 Posts
    hi Mike_O,
    thanks for the reply... i tried to print the sql statement and run in into the database and it works fine...
    can you give me a sample codes where in you get some data from the database server then print it in the web page using <asp:label> or something like my scenario?
    I am sorry because I am new to asp.net and I really find it hard to catch up. I also try to research about this problem but still I was not able to find the correct solution.

  • #4
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Okay, let's say that your SQL statement produces 1 row and value of column [logs] is what you need to put into label MyLabel. You can* do it this way. And I say can* because:
    #1. There are various ways of doing this and this is only an example.
    #2. Sometimes another guy comes along and says "oh no, don't do it this way, do it my way...my way is better...bla bla bla". No, at this point, your goal is not to look for which way is better, but to familiarize yourself with all the common approaches because once you know this, then you can make your own decision as to which way is better. So, to start, just read about the objects you see below (SqlConnection, SqlCommand, SqlDataReader). Also read about using the try..catch statement, and using{...}, which is basically a neat way of automatically doing garbage collection (a feature of C#). After that, read about binding data to data controls.

    Label control:
    Code:
    .
    .
    .
    <asp:Label ID="MyLabel" runat="Server" />
    .
    .
    .
    Codebehind:
    Code:
    using System.Data.SqlClient;
    .
    .
    .
    try
    {
       using (SqlConnection mySqlConnection = new SqlConnection("<my Connection String>"))
       {
          mySqlConnection.Open();
          using (SqlCommand mySqlCommand = new SqlCommand("<my sql statement>", mySqlConnection))
          {
             mySqlCommand.CommandType = CommandType.Text; 
             using (SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
             {
                if (mySqlDataReader.HasRows)
    	    {
                   mySqlDataReader.Read();
                   MyLabel.Text = mySqlDataReader["logs"].ToString();
                }
             }
          }
          mySqlConnection.Close();	      
       }
    }
    catch(Exception ex)
    {
       Response.Write(ex.Message);
    }
    .
    .
    .

  • Users who have thanked Mike_O for this post:

    newbb (05-10-2011)

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 2 Times in 2 Posts
    okey thanks... I just want to ask what does "exception" really means?? it makes me confused...because when i tried to read about try and catch it talks more about exception and i don't know what that really means... I mean.. what are the values that you can have "exception"? when do we need to use exception???

  • #6
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Hi newbb,

    The Exception class represents run-time errors. In most situations, it is a good practice to surround your code with a try...catch in order to handle (as in the term "exception handling") run-time errors. As you will see, there are actually many different kinds of such classes which all derive from the Exception class. Also, you can throw (or generate) exceptions. You can read about that too.

    Here is an MSDN link that explains it: http://msdn.microsoft.com/en-us/libr...exception.aspx
    In fact, always try to use MSDN first to look up information relating to any class.

    Mike

  • #7
    New Coder
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 2 Times in 2 Posts
    hi mike_o,

    that was quite a long subject to understand.... but now i already get the concept of it. I also conclude that may codes are locking. I put try and catch as expected and i got no error at all, therefore the problem is how i pass the value of the sql statement, then use I get and set to have the date of the last log in of the user....

    Now my page is already working and I am starting to love ASP.net...... Thanks for your help!!!!

    Best regards,

    newbb


  •  

    Posting Permissions

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