Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a web page that contain dropdownlist, session[student-id], button, gridview

student will choose the course from the dropdownlist and click on the register button then the student-id and the selected course will be added to the gridview.

My question is how can I show error message for the student if he has already register this course.

I mean I want the register button to check in the database if this student has the selected course.

Any help??

What I have tried:

for (int i = 0; i < GridView1.Rows.Count; i++)
            {

                GridViewRow row = GridView1.Rows[i];


                if (row.Cells[0].Text == DropDownList2.SelectedValue.ToString())
                {
                    //display error message
                }

                else
                {
                  // I have the code for adding the data to gridview and it works fine

                }
Posted
Updated 19-Apr-17 10:47am
v2
Comments
CHill60 19-Apr-17 6:00am    
The "What I have tried" section is where you should put the code that you have tried.
Do you want to check the GridView or the Database - the point where you are adding data to the database becomes relevant.
As things stand we do not have enough information to help you
R.M49 19-Apr-17 16:50pm    
I added what code I have tried
Sinisa Hajnal 19-Apr-17 6:13am    
Check the gridview for that course ID? You shouldn't go to the server for such basic validation. You should, of course, validate again on the server, but the check for user message should be client-side

1 solution

You should have to check the values before inserting it into a database

You can check it by that way.


SQL
IF EXISTS(SELECT COLUMN1 FROM TABLENAME WHERE COLUMN1 = @COLUMN1 AND COLUMN2 = @COLUMN2)
	BEGIN
		SELECT 'This student registered already' 
	END
ELSE
	BEGIN
		INSERT INTO TABLENAME (COLUMN.......)  VALUES (@COLUMN........)
		SELECT 'Student registered successfully'
	END


You have to display message from SQL to Label control in c#
 
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