Click here to Skip to main content
15,889,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i want to add only five subject marks for each student...how i will restrict an Stduent_ID ?if Stduent_ID=1 and inserted five times then for six entry i should not add...how i will do it in C# + SQL...

What I have tried:

Declare @Counter int
Set @Counter=(SELECT Count(Student_ID) as 'StudentCount'
FROM CourseSemOne
where Student_ID=3 Group by Student_ID )
if(@Counter >= 6)
print'Sorry! You cannot add more than five subject data for a single stduent'
else
print 'Insert Query'
Posted
Updated 22-Jun-16 22:18pm

minor correction

SQL
if(@Counter >= 5)
 
Share this answer
 
Comments
Member 12388304 23-Jun-16 0:04am    
not working for >= 5
Karthik_Mahalingam 23-Jun-16 0:24am    
not working means, is it adding a new record?

try this
Declare @Counter int
Set @Counter=(SELECT Count(*) FROM CourseSemOne where Student_ID=3 )
if(@Counter >= 5)
print'Sorry! You cannot add more than five subject data for a single stduent'
else
print 'Insert Query'
string sql = "SELECT Count(Student_ID) FROM CourseSemOne where Student_ID='"+Student_ID+"' Group by Student_ID ";
SqlConnection con = new SqlConnection(@"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=CS_DB;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();

if (count <5 && count >=0)
{
obj.insertSemCourseOne(Student_ID, Course_Code, Course_Title, Total_Marks, Obtain_Marks, Grade, Value, Cr_Hours, Grade_Point, GPA, CGPA);
// DatabaseConnnectionClass.UserMessage("Added");
MessageBox.Show("Added Subject");
Dataloaddd();
}
else
{

MessageBox.Show("Sorry!For this Student ID = "+Student_ID+" You cannot add more than 5 data.");
}
 
Share this answer
 
v2

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