-1

I have a question regarding the use of the Textbox in ASP.NET.

I have a download page with a button, so that when I click, the action of downloading data from a database(Mysql) to a database(Sql) is done. On the default page I have a TextBox field that has to display the last execution date of the download action.

How can i intercept that last execution date?

default page:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
       *// last execution date capture and display*
    }
}

download page

protected void Button1_Click1(object sender, EventArgs e)
{
    string server = "XXX";
    string database = "XXX";
    string uid = "XXX";
    string password = "XX";
    bool convert =true;
    string MySqlConnStr = "SERVER=" + server + ";" + "DATABASE=" +
    database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";" + "ConvertZeroDateTime=" + convert +";";
    string SqlConnStr = "Data Source=XXX;" + "Initial Catalog=XXX;" + "User id=XXX;" + "Password=XX;";

    DataSet SqldSet = new DataSet(); // SqlServer Dataset that holds Sql Server data

    DataSet MySqldSet = new DataSet(); //MySql dataset that will be used to push data into MySql database

}
  • Please write a better title based on your specific problem http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title – Soner Gönül Apr 20 '15 at 11:34
  • This has nothing to do with MVC (I have removed the tag) –  Apr 20 '15 at 11:36
  • I think this help you http://stackoverflow.com/questions/16550703/sql-get-the-last-date-time-record – Pradnya Bolli Apr 20 '15 at 11:38
  • do you want to store the last execution date information to database and display every-time user opens the application? or just display during the session? – ANewGuyInTown Apr 20 '15 at 11:41
  • Do you require to know the historic datetime for downloads done after a page refresh, or do you only care about the downloads done in that session? – Dominic Scanlan Apr 20 '15 at 11:43
  • i want to display it every time user opens the application. – MrMartinico1988 Apr 20 '15 at 11:43
  • Why do you want to "display" information in `Textbox`? Textbox is to input field. You should choose a label or textblock to display info. Also why are you trying to display last execution date in `Text_Changed` event? This event only fires when you input some text in the textbox. – ANewGuyInTown Apr 20 '15 at 11:43
  • just care about the downloads done in that session. – MrMartinico1988 Apr 20 '15 at 11:44
  • ok! great. so i should change the Textbox to label field. But the question is always how can i capture the download execution date and display it? – MrMartinico1988 Apr 20 '15 at 11:46
  • See my answer below. You should first query the `MSSQL` database to get the last updated value and assign it to the label. – ANewGuyInTown Apr 20 '15 at 11:53
  • Absolutely the worst title I've seen all month. How many questions a day do you think are about text boxes in ASP.NET? – John Saunders Apr 20 '15 at 11:53
  • Sorry guys for the title. this was my first ever post on this sites. and i am not english native. Hope i will do better the next time. thank u for answering at all. – MrMartinico1988 Apr 20 '15 at 11:59
  • @ANewGuyInTown can i know how wich method to use to catch that value in asp.net? how to implement the c# function i mean. – MrMartinico1988 Apr 20 '15 at 12:03

2 Answers2

2

If you want to know the date when it was last updated (i.e. when was the last time the sql table was uploaded information from mysql, then use the following query to retrieve the information from MsSql database.

SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
    FROM sys.dm_db_index_usage_stats
         WHERE database_id = DB_ID( 'YourDatabase')
          AND OBJECT_ID=OBJECT_ID('TableNameThatGetsUpdatedOrInserted')

Assign the last_user_update value to the label.

Use the following C# code:

  public DateTime GetLastUpdatedDate()
        {
            string connectionString="";//your connection string to connect to Ms Sql database 
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandText = @" SELECT top 1 last_user_update
                                        FROM sys.dm_db_index_usage_stats
                                        WHERE database_id = DB_ID( 'YourDatabase')
                                        AND OBJECT_ID=OBJECT_ID('TableNameThatGetsUpdatedOrInserted')";
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandTimeout = 60;
                    return (DateTime)cmd.ExecuteScalar();
                }
            }
        }
ANewGuyInTown
  • 5,957
  • 5
  • 33
  • 45
  • @AnewGyInTown. Thanks its works very well! to do the same operation in the opposite side (from Sql to Mysql) in a update query on Mysql, the variable cmd.CommandText should be modify in which way? – MrMartinico1988 Apr 20 '15 at 13:50
0

for updates to the textbox when clicking the button

protected void Button1_Click1(object sender, EventArgs e)
{
     TextBox1.Text = DateTime.Now.ToString("dd-mm-yyyy hh:MM:ss")
}

for the page load you'll have to get the datetime from the database and putting that in

protected void Page_Load(object sender, EventArgs e)
{
    //call to database
    TextBox1.Text = returned date.
}
Dominic Scanlan
  • 1,009
  • 1
  • 6
  • 12