在gridview C#ASP.NET中防止重复数据 [英] Prevent duplicate data in gridview C# ASP.NET
问题描述
我有一个网页,其中包含下拉列表,会话[student-id],按钮,gridview
学生将从下拉列表中选择课程并点击注册按钮然后学生ID和所选课程将被添加到gridview。
我的问题是如果他已经已经为学生显示错误信息注册这门课程。
我的意思是我希望注册按钮检查数据库中是否有这个学生选择的课程。
任何帮助??
我尝试过:
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
}
推荐答案
在将值插入数据库之前,您必须检查这些值
您可以查看通过这种方式。
You should have to check the values before inserting it into a database
You can check it by that way.
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
您必须在c#中显示从SQL到Label控件的消息
You have to display message from SQL to Label control in c#
这篇关于在gridview C#ASP.NET中防止重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!