逻辑问题再次出现 [英] problem again about logic

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

问题描述

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)

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;">

           <asp:Label ID="Label1" runat="server" Text="SudentRegistration"></asp:Label>
           <br />

           <br />
           <asp:Label ID="Label2" runat="server" Text="StudentId" AssociatedControlID="TextBox1"></asp:Label>
                  <asp:TextBox ID="TextBox1" runat="server" height="19px"

               style="position: relative" ontextchanged="TextBox1_TextChanged"></asp:TextBox>



           <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>
                  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

           <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>

           <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

           <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"

               ControlToValidate="TextBox3" ErrorMessage="DOB is Required"

               ValidationGroup="Student Registration">*</asp:RequiredFieldValidator>
           <br />

           <br />


           <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">

       <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>
                   <asp:TextBox ID="TextBox4" runat="server" height="19px"

           ontextchanged="TextBox1_TextChanged" style="position: relative"></asp:TextBox>

       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"

           ControlToValidate="TextBox4" ErrorMessage="Id is Required"

           ValidationGroup="Student Certificates">*</asp:RequiredFieldValidator>
       <br />
        <br />
       <asp:Label ID="Label7" runat="server" Text="CertificateId" AssociatedControlID="TextBox7"></asp:Label>
                <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>

       <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>
         <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>

       <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>
           <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>

       <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"

           ControlToValidate="TextBox7" ErrorMessage="CetificateDate is Required"

           ValidationGroup="Student Certificates">*</asp:RequiredFieldValidator>
       <br />
       <br />
       <br />

       <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


 <pre lang="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 APPLY CONDITION?



MY PROBLEM IS THAT HOW I APPLY CONDITION?

limit the  records entry to maximum of 5 records.

推荐答案

The question is so simple, there was no need to show a whole book of code. Just do this before the INSERT in your Stored Procedure.

The question is so simple, there was no need to show a whole book of code. Just do this before the INSERT in your Stored Procedure.

DECLARE @StudentId = 10
DECLARE @CertCount int
SELECT @CertCount = COUNT(*) FROM StudentCertificates WHERE StudentID = @StudentID

IF (@StudentId >=5) 
BEGIN
RAISERROR('Only 5 certificates allowed per student', 1, 1);
END


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

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