A new record inserted into DB on each refresh.

This is the click functionality in the code behind on the send button that submits the form.

Private Sub ibtnfrmSend_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnfrmSend.Click

        Dim lReturn As Boolean
        pi_obj_Message = New Trade_BL.Message
            lReturn = pi_obj_Message.PostMessage(Session(Session_str_UserName), txthTo.Value, txtSubject.Text, Server.HtmlEncode(fckEditor.Value))
        Catch ex As Exception
            Exit Sub
        End Try
        lblMsg.Text = "Message sent successfully"
        lblMsg.Visible = True

End Sub

this is the implementation in the business logic.

Public Function PostMessage(ByVal prm_Sender_ID As String, ByVal prm_To_ID As String, ByVal prm_Subject As String, ByVal prm_Content As String) As Boolean
        Dim idbparameter(4) As IDataParameter
        Dim idb_Member_Cat_parameter(1) As IDataParameter
        Dim dt_row As DataRow
        Dim objTransaction As IDbTransaction
        Dim objConnection As IDbConnection

        objConnection = pi_objHelper.GetConnection(GlobalData.ConnectionString)
        objTransaction = objConnection.BeginTransaction()
        idbparameter(0) = GlobalData.DataHelper.GetParameter("@sender_id", DbType.String, 20, ParameterDirection.Input)
        idbparameter(0).Value = prm_Sender_ID

         .. other parameters..

            pi_objHelper.ExecuteNonQuery(objTransaction, CommandType.StoredProcedure, "USP_INSERT_MESSAGE", idbparameter)

        Catch ex As Exception
            Return False
        End Try
        Return True

    End Function

when i insert the first record, the form is hidden and the grid on the same page is displayed.. but does not show the new posting. lets assume .. an member sends an email to himself... once you submit the form the record is inserted.. but the grid that pulls data in the page_load of the same page.. does not show it.. if i try to reload the page.. to force datagrid to pull data.. the entire form submission takes place again.. and a new record is inserted into the db. it keeps happening till the time you goto other page and then come back.

Who is Participating?
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
Hi asifthakur,

Ok, so the reason is that page_load is executed first then your button click event. You need to move the code that populates the grid out of page_load into a seperate sub (you may already have this of course) and call this after the ClearForm() call in the click event. This way your grid will get the latest data including the posted message.

From what you say, there is really no need to have the grid populating in page_load anyway because it is not displayed initially, only on the postback. Therefore as long as it is called at some point (i.e. in the click event) it will be fine.

Tim Cottee
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.