To determine whether the page has actually been submitted or whether it has just been refreshed. Alternatively, once the user has clicked save, they are redirected to another page, just a thought.
ok , is there ayway to disbale the refresh button form the browser at this page andalso F5 , am with you that the redirect page is better , but jus asking ... if i can do it or not ,,
I am not aware of a way of doing this, but it is definitely not something that you want to base your logic on. Rather, use the re-direct as suggested by myself and mend, or use the IsPostBack method to decide what should be done.
You can't disable F5 after the form has been submitted, but if you really want to stay on the same page, then you can (in the button click) process the details, redirect to the same page so that it's "fresh".
gep13 and mend thanks for your help , i was wondering if i can disable the F5 by any javascript in the code-behind page , but i'll take what you were suggested cuz i believe it's better as you said .. i'll come back soon
still didnt get how i can wirte code to let user insert his survey's question and all questions will be under one SurveyID . i think i had confused abou how i can link the question table with the survey table & user table within the code
Last edited by alqous; Apr 21st, 2009 at 06:31 PM.
You can do this based on your database design. Your database design, IIRC, allowed for one SurveyID to be linked to several QIDs. You need to make entries in that linked table. It's just an INSERT statement, try it out, show us where you get stuck.
Protected Sub btnsavesurvey_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsavesurvey.Click
cs.conn.Open()
Dim comm As New SqlCommand
comm.CommandType = CommandType.StoredProcedure
comm.CommandText = "SurveyIDTBLCONN"
Try
Select Case ddlquestionnaire.SelectedIndex
Case Is = 1
Dim cmd As New SqlCommand("insert into Questions(Question,SID,AnswerTypeID)values('" & txtq.Text & "','" & "@SurveyID" & "','" & 1 & "')", cs.conn)
cmd.ExecuteNonQuery()
MsgBox("تمت اضافة السؤال", MsgBoxStyle.Information, " احصائيات اون لاين")
Case Is = 2
Dim cmd As New SqlCommand("insert into Questions(Question,AnswerTypeID)values('" & txtq.Text & "','" & "@SurveyID" & "','" & 2 & "')", cs.conn)
cmd.ExecuteNonQuery()
MsgBox("تمت اضافة السؤال", MsgBoxStyle.Information, " احصائيات اون لاين")
Case Is = 3
Dim cmd As New SqlCommand("insert into Questions(Question,AnswerTypeID)values('" & txtq.Text & "','" & "@SurveyID" & "','" & 3 & "')", cs.conn)
cmd.ExecuteNonQuery()
MsgBox("تمت اضافة السؤال", MsgBoxStyle.Information, " احصائيات اون لاين")
Case Is = 4
Dim cmd As New SqlCommand("insert into Questions(Question,AnswerTypeID)values('" & txtq.Text & "','" & "@SurveyID" & "','" & 4 & "')", cs.conn)
cmd.ExecuteNonQuery()
MsgBox("تمت اضافة السؤال", MsgBoxStyle.Information, " احصائيات اون لاين")
End Select
Catch ex As Exception
MsgBox(ex.ToString)
End Try
cs.conn.Close()
cleartextboxz()
Response.Redirect("addsurvey.aspx")
End Sub
and this is the stored procedure that i created :
Code:
ALTER PROCEDURE dbo.SurveyIDTBLCONN
AS
/* SET NOCOUNT ON */
Declare @SurveyID Int
Begin
Set @SurveyID = Scope_Identity()
Insert into Questions (SID) values (@SurveyID)
End
but still doesnt work , can't link the surveyID between the Question table and the survey table , could ypu plz help me ..
Can I ask why you are using a case statement in the above code?
You are using the exact same query in each case with the exception of the survey ID. Why not use a parameter in the query string, and the value of that parameter will be ddlquestionnaire.SelectedIndex.
Related to that, you are concatentating the SQL string and building up the SQL string, this really should be avoided as it leaves you open to SQL Injection. To prevent this, you should always use parameterized queries. You can find an example of how to do this in this thread:
Right, so what you do is to create a new survey, insert a new row into Survey. SID, which should be autoincrement, should return a value to you, the new primary key. Use that to insert into Questions along with the new questions.
Can I ask why you are using a case statement in the above code?
am using case statment cuz i have different answertype everyone of it has there # , if you look at the end of each statment u'll find that all three are not the same ... but thanx for the hint and i'll try to do it by parameters to avoid Sql injection
i'm little confused .. and i didnt get what you meant exactly .
did you meant the SID in question should be autoincrement?
I'm choosing the answertype from dropdown list and type the question then insert it in the database .. but at everytime i can insert one question then return back to insert the second one ..etc , after that i should generate a page that has the survey depend on what was insert
am still confused about how to link the SID in Q table with SID in survey table , and also how i can let multiple qusetion has one SID ..
am using case statment cuz i have different answertype everyone of it has there # , if you look at the end of each statment u'll find that all three are not the same ... but thanx for the hint and i'll try to do it by parameters to avoid Sql injection
thanx agian
Hey,
But this is what I am talking about. Your answertype is exactly what the value of ddlquestionnaire.SelectedIndex is. i.e. within your case statement you do something like this:
If ddlquestionnaire.SelectedIndex is 1, you put 1 in the SQL Query, if it is 2 you put a 2 in the query etc so in the same way as you add txtq.Text into the query, you can simply add ddlquestionnaire.SelectedIndex as well, the case statement is completely unnecessary.
So your code would become:
Code:
Dim cmd As New SqlCommand("insert into Questions(Question,SID,AnswerTypeID)values(@QuestionText,@SurveyID, @AnswerTypeID)", cs.conn)
Where
@QuestionText is a parameter with a value of txtq.Text
@SurveryID is a parameter with a value of (not sure where this comes from in your code)
@AnswerTypeID is a parameter with a value of ddlquestionnaire.SelectedIndex
First, do an INSERT into the Survey table. Then do a select on SCOPE_IDENTITY(), this will return to you the ID of the new survey row. (This has to be done in a stored procedure).
Then, take that new Survey ID and everytime you INSERT into the questions table, include the SID as one of the values, which is this statement
AS
SET NOCOUNT ON
insert into SURVEY (UserID) values ('1')
Declare @SurveyID Int
Begin
Set @SurveyID = Scope_Identity()
Insert into Questions (SID) values (@SurveyID)
End
is it right ? i feel there is some thing wrong with it , am trying to figure out how to insert the userid in the surveytable withing the stored procedure.
You can't insert into Questions if you don't have any questions. From this stored proc, you must return @SurveyId and hold on to that value in your code. When it's time to insert a new question, that's when you can use @SurveyId in the next stored procedure.
Code:
Declare @SurveyID Int
insert into SURVEY (UserID) values (@UserId)
Set @SurveyID = Scope_Identity()
SELECT @SurveyID
@UserId int
AS
SET NOCOUNT ON
Declare @SurveyID Int
insert into SURVEY (UserID) values (@UserId)
Set @SurveyID = Scope_Identity()
SELECT @SurveyID
and my code is became :
Code:
cs.conn.Open()
Dim comm As New SqlCommand
comm.CommandType = CommandType.StoredProcedure
comm.CommandText = "SurveyIDTBLCONN"
Try
If (txtq.Text <> "" AndAlso ddlquestionnaire.SelectedIndex <> "0") Then
' Insert Query using stored procedure
Dim cmd As New SqlCommand("insert into Questions(Question,SID,AnswerTypeID)values(@Question,@SurveyID,@AnswerTypeID)", cs.conn)
cmd.Parameters.AddWithValue("@Question", txtq.Text.Trim)
cmd.Parameters.AddWithValue("@AnswerTypeID", ddlquestionnaire.SelectedIndex)
cmd.Parameters.AddWithValue("@SurveyID", comm.ExecuteScalar)
cmd.ExecuteNonQuery()
Else
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
cs.conn.Close()
cleartextboxz()
Response.Redirect("addsurvey.aspx")
it's not working i dont now why ?
and also could you tell me what is the way to let the survey belong to their users , so when the user login his CP page he can veiw his survey??
thanx for everything ..
TC
Last edited by alqous; May 18th, 2009 at 02:47 PM.