2

I have an sql statement that I posted on this site, and I applied in my website that works and inserts users name and information into the database soon after the user presses the submit button.

This however doesnt happen when I set the the submit's buttons PostBackUrl property. When I do this, no data is inserted, and the whole function that holds the execution of the insert statement seems to be skipped (as I tried to make a mistake on purpose,,and no exception was thrown).

How can i make the the postBackUrl work, so that data will have the time to be inserted?

working sqlStatment:

 insertCommand.Append("DECLARE @TopicsId int; INSERT INTO Topics(Theme,Topics,Date)");
    insertCommand.Append("VALUES(@topic,@subTopic,GETDATE())");
    insertCommand.Append("SET @TopicsId = SCOPE_IDENTITY()");

    insertCommand.Append(" INSERT INTO Threads(UsersID,TopicsID,Date,ThreadTitle,ThreadParagraph,ThreadClosed,Views,Replies,PageNumber)");
    insertCommand.Append(" SELECT @uniqueIdentifier,@TopicsID,GETDATE(),@questionTitle,@questionParagraph,0,0,0,FLOOR(Count(t.TopicsID)/20)");
    insertCommand.Append(" FROM Threads AS d INNER JOIN Topics AS t ON d.TopicsID=t.TopicsID");

working when:

<asp:Button ID="sendButton1" runat="server" Text="שלח" Width="60px" 
            onclick="sendButton1_Click"  />

non working sqlStatement when:

<asp:Button ID="sendButton1" runat="server" Text="שלח" Width="60px" 
            onclick="sendButton1_Click" PostBackUrl="~/AnswerQuestion.aspx" />
Curtis
  • 101,612
  • 66
  • 270
  • 352
Matrix001
  • 1,272
  • 6
  • 30
  • 51
  • 1
    Please post your code, so that we can figure out your issue. – Muhammad Akhtar Jun 13 '11 at 06:27
  • updated see above. when i do postBackURL..it seems that there is no time to insert data..and it goes fast to post url, and skips the insert sql statement – Matrix001 Jun 13 '11 at 06:40
  • That second `INSERT` looks suspect. To my eyes, it will duplicate every row currently in the `Threads` table, but with the new `@TopicsID` value for `TopicsID`. Is that what you want? – Damien_The_Unbeliever Jun 13 '11 at 06:53
  • What i want is to cross post the page after the submit click event.. sql statemetn works..the click event and postbackurl dont work well together..so i seperated them..i dont know when to trigger and make it pass to a different page – Matrix001 Jun 13 '11 at 07:01

1 Answers1

1

This will cause cross-page postback. It will post the current page to the provided URL in the button's PostbackURL property when the Button control is clicked. Your button Click event will not be triggered in this case.

For Details please check from MSDN

EditL Why not attempt this simply, like...

protected void Button1_Click(object sender, EventArgs e)
{
///.....Your Code.....
insertCommand.Append("DECLARE @TopicsId int; INSERT INTO Topics(Theme,Topics,Date)");
insertCommand.Append("VALUES(@topic,@subTopic,GETDATE())");
insertCommand.Append("SET @TopicsId = SCOPE_IDENTITY()");

insertCommand.Append(" INSERT INTO Threads(UsersID,TopicsID,Date,ThreadTitle,ThreadParagraph,ThreadClosed,Views,Replies,PageNumber)");
insertCommand.Append(" SELECT @uniqueIdentifier,@TopicsID,GETDATE(),@questionTitle,@questionParagraph,0,0,0,FLOOR(Count(t.TopicsID)/20)");
insertCommand.Append(" FROM Threads AS d INNER JOIN Topics AS t ON d.TopicsID=t.TopicsID");

 ///..At the end add this......
 Response.Redirect("~/AnswerQuestion.aspx");
}
Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • finally { sqlConnect.Close(); HttpContext.Current.Server.Transfer("~/AnswerQuestion.aspx"); } even when i do this, without the postbackurl property. it wont insert my data..why – Matrix001 Jun 13 '11 at 06:47
  • How can i set it so that the click event triggers and another event to send the page is triggered next...? – Matrix001 Jun 13 '11 at 07:00
  • use Response.Redirect("~/AnswerQuestion.aspx"); at the end of code, I have just updated my answer. you can get idea now and remove the postback URL. – Muhammad Akhtar Jun 13 '11 at 07:07
  • Your answer is correct. Thats what i did before you posted ur comment.. i used Server.Transfer. it all works now – Matrix001 Jun 13 '11 at 07:32