tags:

views:

29

answers:

1

hi,

i am beginner with sql,

I have comments Template with button in the end of it to add the new comment to the database ,so i use SQL INSERT code to add and it works fine. but after button postback , the the new comment does not shows in my label until i refresh the page manually! .

this my code in c# code portion:

    protected void Page_Load(object sender, EventArgs e)
    {

        ViewComments();

    }

    private void ViewComments()
    {
        hookUp =
            new SqlConnection("Server=xxxx\\SQLEXPRESS;Database=WebExcerciseDB;" + "Integrated Security=True");
        SqlCmd = new SqlCommand("SELECT PersonName,PersonMail,Comment FROM CommentsTable", hookUp);
        try
        {
            hookUp.Open();

            reader = SqlCmd.ExecuteReader();
            while (reader.Read())
            {
                SQL_Result.Text += reader["PersonName"] + "  " + reader["PersonMail"] + "  " + reader["Comment"] +
                                   "<br/>";
            }

        }
        catch (Exception)
        {
            MessageBox.Show("Error in database code\n");


        }
        finally
        {
            reader.Close();
            hookUp.Close();
        }
    }

    protected void AddCommentBtn_Click(object sender, EventArgs e)
    {
        // SQL First INSRET Way //
        //string InsertSQLString = "INSERT INTO CommentsTable(PersonName,PersonMail,Comment,PostDate) VALUES ('" +
        //                         PersonName.Text + "','"+ PersonMail.Text + "','" + PersonComment.Value + "','" +
        //                         DateTime.Now + "')";
        //hookUp =
        //               new SqlConnection("Server=xxxx-PC\\SQLEXPRESS;Database=WebExcerciseDB;" + "Integrated Security=True");
        //SqlCmd = new SqlCommand(InsertSQLString, hookUp);
        //hookUp.Open();
        //SqlCmd.ExecuteNonQuery();
        //hookUp.Close();

        // SQL First INSRET Way End //


        // SQL second INSRET Way  //

        string InsertSQLString = "INSERT INTO CommentsTable(PersonName,PersonMail,Comment,PostDate)VALUES(@Name,@Mail,@Comment,@Date)";

        hookUp =
                 new SqlConnection("Server=xxxx-PC\\SQLEXPRESS;Database=WebExcerciseDB;" + "Integrated Security=True");

        SqlCmd = new SqlCommand(InsertSQLString, hookUp);
        SqlCmd.Parameters.Add("@Name", PersonName.Text);
        SqlCmd.Parameters.Add("@Mail", PersonMail.Text);
        SqlCmd.Parameters.Add("@Comment", PersonComment.Value);
        SqlCmd.Parameters.Add("@Date", DateTime.Now);

        hookUp.Open();
        SqlCmd.ExecuteNonQuery();
        hookUp.Close();

        // SQL second INSRET Way End  //








    }
}

}

thanks any way..

A: 

the reader is not the problem. The problem is order in which your methods are called.

this should help: http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events

liho1eye
thanks ikrz for replay...i tried to use OnLoad Event but it does not works in correct way ..please help..
samih
try moving the `ViewComments();` call out of the Load event and into the LoadComplete event
liho1eye
protected void LoadComplete(EventArgs e) { ViewComments(); }is this the write format for LoadComplete event
samih
simply rename `Page_Load` into `Page_LoadComplete` - that should do it.If not, the go to you page's constructor and see if there is something like `this.LoadComplete += new EventHandler(Page_LoadComplete);` in there and add it otherwise
liho1eye
It works!... thanks alot ikrz...I should learn about lifecycle events.
samih