Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have table Answer colums are
ans_id identity key //primary key
question_id foreign key
student_id foreign key
answer varchar(500)

so how to query or any kind of setting needed
i am developing application ASP.Net with C#
Posted
Comments
syed shanu 20-Mar-14 1:46am    
so what you want .In your Answer thble you have question_id and student_id .And i belive that you will be having question Table and Student table.And i think you will have student details tabel where you will have each student matching question with question id.After student enter the anser in save you will insert the student_id,Question_id and answer to Anser table and create one answer_id for that.During save you need to check the answer table weather for that student already question id is inserted or not if not then do save.Whats problem in this .
swati gapat 20-Mar-14 4:47am    
yes i want to do this but i don't know how to do it
swati gapat 20-Mar-14 4:52am    
yes i want to do this but don't know how to do?
if student double click on insert button question_id will store two times in Answer table but this should not happen..so how can I overcome this problem..please reply
syed shanu 20-Mar-14 5:02am    
you should create a stored procedure for insert and in SP you should check the table which already has qustion id or not .if question id not exist then insert else dont insert .google about creating Stored procedure and check eixiting record
swati gapat 20-Mar-14 9:03am    
thanks

In Table Designer
Select question_id
And student_id
And Right Click on Left Side Of Designer With Your Mouse
To Open Context Menu To Set Primary Key.



Once You Set Primary Key,
Now Only Distinct Value Can Be Save .
 
Share this answer
 
See I have written a storeprocedure it should something like this I don't know your table name, I just studied your conversation and procedure something like this
create procedure usp_QuestionAnswer
{
@question_id int=0 ,
@student_id int =0,
@answer varchar(500)=null
}
AS BEGIN
//Check if data exists or not
if not exists(select question_id from tblQuestionAnswer where question_id = @question_id  and  student_id =@student_id  ) begin
   insert into tblQuestionAnswer(question_id ,student_id,answer)
   values(@question_id ,@student_id,@answer)

end

END
 
Share this answer
 
Comments
swati gapat 20-Mar-14 9:57am    
but i don't know where to write in my program
when i am writing on click of button,
program like create procedure it giving me an error of missing namespace or directive.
tell how to write i am using C#
[no name] 20-Mar-14 12:59pm    
if you don't know uses of storeprocedure. And you want use only c# code behind then do it like something below
string query="if not exists(select question_id from tblQuestionAnswer where question_id =@question_id and student_id =@student_id ) begin insert into tblQuestionAnswer(question_id ,student_id,answer) values(@question_id ,@student_id,@answer)";
SqlCommand newCommand=new SqlCommand(connection,query);

Hope so other things you know it very well
swati gapat 20-Mar-14 14:44pm    
but if record exist .. how to show error message
how to show output text as answers are already inserted
swati gapat 20-Mar-14 14:47pm    
thanks..code is working

but how to show message that record is already exist
in procedure or any othre way
[no name] 21-Mar-14 1:35am    
great! And you can mark me as your answer.
if exists(select question_id from tblQuestionAnswer where question_id = @question_id and student_id =@student_id) begin
select * from tblQuestionAnswer where question_id = @question_id and student_id =@student_id end

You can do like this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900