Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m devloping timetable as my project. i wanted to compare two field timeslots and class. if they are already available in database then print approprite message otherwise it will store in database. pls help...
Posted
Updated 30-Jan-12 1:51am
v3
Comments
OriginalGriff 30-Jan-12 7:24am    
And what have you tried?
Where are you stuck?
Jignesh J patel 30-Jan-12 7:33am    
String str = "insert into TIMETABLE values ('" + SUBJECT_CODE.Text + "','" + OFFER_ID.Text + "','" + CLASS_ID.Text + "','" + DropDownList1.SelectedItem + "','" + TIME.Text + "','" + FACULTY_ID.Text + "','" + DropDownList2.SelectedItem + "','" + DURATION.Text + "') ";
cmd = new SqlCommand(str, con);
dr = cmd.ExecuteReader();
if (dr[2] == CLASS_ID.Text && dr[4] == TIME.Text && dr[3] == DropDownList1.SelectedIndex)
{
Response.Write(" Slot Already Given Please Choose another Class");
}
OriginalGriff 30-Jan-12 7:43am    
Yes?
And?
Apart from "Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead." what do you expect us to say?
What does it do that it shouldn't, or not do that it should?
Sanjay K. Gupta 30-Jan-12 7:28am    
First try yourself and give the problem details,if you faced.
manognya kota 30-Jan-12 7:30am    
Where are you comparing? Are you getting the data to front end or comparing in the back end?

Hi,

From the code, you can pass the 2 variables as parameter to SP and get the count of rows in the DB.If the records exists return 1 else insert return 0 from the SP.

OR use a merge statement.

XML
MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition>
[WHEN MATCHED
THEN <merge_matched> ]
[WHEN NOT MATCHED [BY TARGET]
THEN <merge_not_matched> ]
[WHEN NOT MATCHED BY SOURCE
THEN <merge_ matched> ];


Refer the below link for example

Merge Statement in SQL Server 2008[^]

Hope this helps.
 
Share this answer
 
this may not be 100% currect but it will give some idea

C#
cmd = new SqlCommand("select * from timetable where clo1='"+CLASS_ID.Text+"' and col2='"+TIME.Text+"'", con);
 dr = cmd.ExecuteReader();
if(!dr.HasRows)
{
	String str = "insert into TIMETABLE values ('" + SUBJECT_CODE.Text + "','" + OFFER_ID.Text + "','" + CLASS_ID.Text + "','" + DropDownList1.SelectedItem + "','" + TIME.Text + "','" + FACULTY_ID.Text + "','" + DropDownList2.SelectedItem + "','" + DURATION.Text + "') ";
            cmd2 = new SqlCommand(str, con);
            cmd2.ExecuteNonQuery();
}
 
Share this answer
 

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