بسم الله الرحمن الرحيم
أخواني الكرام, لدي مشكله بسيطه واحتاج مساعدتكم فيها
عند عمل ادخال لقاعدة البيانات أريد التأكد أولا إذا كان المدخل موجود في قاعدة البيانات أم لا, لدي الكود التالي:
<td align="center">
<div align="center">
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Matric_No"
DataSourceID="SqlDataSource2" EnableModelValidation="True" ForeColor="#333333"
GridLines="None" style="text-align: center">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="R_Email" HeaderText="Student Email"
SortExpression="R_Email" />
<asp:BoundField DataField="Matric_No" HeaderText="Matric No"
SortExpression="Matric_No" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Super1" HeaderText="Super 1 Name"
SortExpression="Super1" />
<asp:BoundField DataField="Super2" HeaderText="Super 2 Name"
SortExpression="Super2" />
<asp:BoundField DataField="Super3" HeaderText="Super 3 Name"
SortExpression="Super3" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [R_Email], [Matric_No], [Name], [Super1], [Super2], [Super3] FROM [Register]">
</asp:SqlDataSource>
<asp:Label ID="Matric0" runat="server"></asp:Label>
<asp:Button ID="Button3" runat="server" Text="Search" />
<asp:Button ID="Button4" runat="server" Text="Register" />
<asp:Label ID="Email0" runat="server" Visible="False"></asp:Label>
<asp:Label ID="Name0" runat="server" Visible="False"></asp:Label>
<asp:Label ID="Super4" runat="server" Visible="False"></asp:Label>
<asp:Label ID="Super5" runat="server" Visible="False"></asp:Label>
<asp:Label ID="Super6" runat="server" Visible="False"></asp:Label>
<asp:Label ID="Pass2" runat="server" Visible="False"></asp:Label>
<asp:Label ID="Label16" runat="server" ForeColor="Red"></asp:Label>
<asp:Label ID="Label17" runat="server"></asp:Label>
</div>
<div>
<asp:GridView ID="GridView3" runat="server">
</asp:GridView>
<asp:SqlDataSource ID="ds1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
<br />
</div>
</td>حيث أن المستخدم يقوم باختيار الصف المطلوب البحث عنه في ملف Excel وبعد الضغط على بحث تظهر النتائج في جدول اخر.. ما اريد عمله هو عند الضغط على تسجيل, يقوم الكود بالتأكد اذا كانت القيمه المدخله موجوده في قاعدة البيانات أم لا
اذا كانت القيمه موجوده فالمفترض أن تظهر رسالة خطأ بأن المدخل موجود في قاعدة البيانات, علما أن ال
Primary key هو الذي يحدد اذا كان المدخل موجود ام لا
Primarykey = Matric_No
بعد البحث عن حل للمشكله وجدت ان الطريقه تكون باضافة Try Catch للتأكد من القيمه قبل ادخالها في قاعدة البيانات , ولكن بعد محاولات كثيره في الكود لم اتوصل الى الطريقه الصحيحه واتمنى الاقي عندكم الحل
كود ادخال البيانات:
ds.InsertCommand = "insert into Student(Matric,Name,Email,Super1,Super2,Super3,Password) values('" + matric.Text + "','" + name.Text + "','" + email.Text + "','" + Super1.Text + "','" + Super2.Text + "','" + Super3.Text + "','" + Pass.Text + "')"
ds.Insert()مع العلم أن رسالة الخطأ هي:
Violation of PRIMARY KEY constraint 'PK_Student'. Cannot insert duplicate key in object 'dbo.Student'.
اسف على الاطاله واتمنى الاقي عندكم المساعده
تقبلوا تحياتي


