关于理解逻辑的问题 [英] problem about understanding logic

查看:59
本文介绍了关于理解逻辑的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中开发了具有两个数据库表的表单
学生

I develop the form in asp.net which have two database tables
Student

StudentId(int,not null)
FullName(nvarchar(30),not null))
DOB(smalldatetime)



学生证书



StudentCertificates

StudentCerificateId(int,not null,pk)
StudentId(int,not null,pk)
CertificateName(nvarchar(30))
CertificateDate(smalldatetime)




下面是代码


page.aspx




Below is the code


page.aspx

<pre lang="xml"><div>
   <script language="javascript" type="text/javascript">
       function validate() {
           if (document.getElementById("<%=TextBox1.ClientID %>").value == "") {
               alert("Student Id is Required");
               document.getElementById("<%=TextBox1.ClientID%>").focus();
               return focus();
           }
  if (document.getElementById("<%=TextBox2.ClientID %>").value == "") {
               alert("FullName is Required");
               document.getElementById("<%=TextBox2.ClientID%>").focus();
               return focus();
       }
    if (document.getElementById("<%=TextBox3.ClientID %>").value == "") {
               alert("DOB is Required");
               document.getElementById("<%=TextBox3.ClientID%>").focus();
               return focus();
       }
   </script>
       <asp:Panel ID="Panel1" runat="server"
           style="position: relative; top: 35px; left: 353px; width: 425px;">
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
           <asp:Label ID="Label1" runat="server" Text="SudentRegistration"></asp:Label>
           <br />
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           <br />
           <asp:Label ID="Label2" runat="server" Text="StudentId" AssociatedControlID="TextBox1"></asp:Label>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;<asp:TextBox ID="TextBox1" runat="server" height="19px"
               style="position: relative" ontextchanged="TextBox1_TextChanged"></asp:TextBox>


           &nbsp;&nbsp;&nbsp;&nbsp;
           <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
               ControlToValidate="TextBox1" ErrorMessage="Id is Required"
               ValidationGroup="Student Registration">*</asp:RequiredFieldValidator>
           <br />
           <br />
           <asp:Label ID="Label3" runat="server" AssociatedControlID="TextBox2"
               Text="FullName"></asp:Label>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
           &nbsp;&nbsp;&nbsp;&nbsp;
           <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
               ControlToValidate="TextBox2" ErrorMessage="Name is Required"
               ValidationGroup="Student Registration">*</asp:RequiredFieldValidator>
           <br />
           <br />
           <asp:Label ID="Label4" runat="server" AssociatedControlID="TextBox3" Text="DOB"></asp:Label>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
           <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
               ControlToValidate="TextBox3" ErrorMessage="DOB is Required"
               ValidationGroup="Student Registration">*</asp:RequiredFieldValidator>
           <br />
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           <br />
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

           <asp:Button ID="Button1" runat="server" Text="Insert" ValidationGroup="Student Registration"  onclick="Button1_Click" />
           <br />
       </asp:Panel>

   </div>
   <asp:Panel ID="Panel2" runat="server"





       style="position: relative; top: 59px; left: 354px; width: 425px; height: 297px">
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
       <asp:Label ID="Label10" runat="server" Text="Student Certificates"></asp:Label>
       <br />
       <br />
       <asp:Label ID="Label6" runat="server" Text="StudentId" AssociatedControlID="TextBox4"></asp:Label>
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<asp:TextBox ID="TextBox4" runat="server" height="19px"
           ontextchanged="TextBox1_TextChanged" style="position: relative"></asp:TextBox>
       &nbsp;&nbsp;&nbsp;
       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
           ControlToValidate="TextBox4" ErrorMessage="Id is Required"
           ValidationGroup="Student Certificates">*</asp:RequiredFieldValidator>
       <br />
       &nbsp;<br />
       <asp:Label ID="Label7" runat="server" Text="CertificateId" AssociatedControlID="TextBox7"></asp:Label>
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
       &nbsp;&nbsp;&nbsp;
       <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
           ControlToValidate="TextBox5" ErrorMessage="CertificateId is Required"
           ValidationGroup="Student Certificates">*</asp:RequiredFieldValidator>
       <br />
       <br />
       <asp:Label ID="Label5" runat="server" Text="CertificateName" AssociatedControlID="TextBox6"></asp:Label>
       &nbsp;&nbsp;<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
       &nbsp;&nbsp;&nbsp;
       <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
           ControlToValidate="TextBox6" ErrorMessage="CertificateName is Required"
           ValidationGroup="Student Certificates">*</asp:RequiredFieldValidator>
       <br />
       <br />
       <asp:Label ID="Label8" runat="server" Text="CertificateDate" AssociatedControlID="TextBox7"></asp:Label>
       &nbsp; &nbsp;&nbsp;<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
       &nbsp;&nbsp;&nbsp;
       <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
           ControlToValidate="TextBox7" ErrorMessage="CetificateDate is Required"
           ValidationGroup="Student Certificates">*</asp:RequiredFieldValidator>
       <br />
       <br />
       <br />
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
       <asp:Button ID="Button4" runat="server" ValidationGroup="Student Certificates"
           Text="Insert" onclick="Button4_Click" />
       <br />
       <br />
       <br />
       <br />

   </asp:Panel>
   <br />
   <br />
   <br />

   <asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="Student Registration" runat="server" />
   <asp:ValidationSummary ID="ValidationSummary2" ValidationGroup="Student Certificates" runat="server" />






page.cs







page.cs


protected void Button1_Click(object sender, EventArgs e)
   {
       DateTime mydate;
       mydate = Convert.ToDateTime(TextBox3.Text);
       SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2010\WebSites\PreInterviewPracticalAssignment\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
       con.Open();
       string que = "insert into Student(StudentId,FullName,DOB) values(@StudentId,@FullName,@DOB)";
       SqlCommand com = new SqlCommand(que, con);
       com.Parameters.Add("@StudentId",System.Data.SqlDbType.Int);
       com.Parameters.Add("@FullName", System.Data.SqlDbType.NVarChar);
       com.Parameters.Add("@DOB", System.Data.SqlDbType.SmallDateTime);
       com.Parameters["@StudentId"].Value = TextBox1.Text;
       com.Parameters["@FullName"].Value = TextBox2.Text;
       com.Parameters["@DOB"].Value = TextBox3.Text;
       com.ExecuteNonQuery();
       con.Close();

   }
   protected void Button4_Click(object sender, EventArgs e)
   {
       DateTime mydate;
       mydate = Convert.ToDateTime(TextBox7.Text);
       SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2010\WebSites\PreInterviewPracticalAssignment\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
       con.Open();
       string que1 = "insert into StudentCertificates(StudentId,StudentCertificateId,CertificateName,CertificateDate)values(@StudentId,@StudentCertificateId,@CertificateName,@CertificateDate)";
       SqlCommand com = new SqlCommand(que1,con);
       com.Parameters.Add("@StudentId", System.Data.SqlDbType.Int);
       com.Parameters.Add("@StudentCertificateId", System.Data.SqlDbType.Int);
       com.Parameters.Add("@CertificateName", System.Data.SqlDbType.NVarChar);
       com.Parameters.Add("@CertificateDate", System.Data.SqlDbType.SmallDateTime);
       com.Parameters["@StudentId"].Value = TextBox4.Text;
       com.Parameters["@StudentCertificateId"].Value = TextBox5.Text;
       com.Parameters["@CertificateName"].Value = TextBox6.Text;
       com.Parameters["@CertificateDate"].Value = TextBox7.Text;
       com.ExecuteNonQuery();
       con.Close();

   }



我的问题是我如何实现这种条件?

提交用于将记录保存到数据库的完整表格时,验证必须确保至少有一个学生证书记录,否则中止保存操作.



my problem is that how i implement this condition?

On submission of complete form for saving record(s) into database, a verification must make sure that at least a single record exists for Student Certificates, otherwise abort the save operation.

推荐答案

在将数据保存到数据库之前,请检查数据库中是否可以计数任何学生证书.如果count == 0,则显示一条消息,否则将其存储
Before saving the data in the database check in the database if you can count any student certicates. If the count == 0 then show a message else store it


这篇关于关于理解逻辑的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆