Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want the number not to be repeated in the database. One of the programmers here wrote me this code and the code works. There is a small obstacle that if it is taken today, it will not be taken tomorrow or on a new date. It appears to me that it exists in the database on a previous date and not a new date.

I want that if taken today at breakfast, the number will not be repeated at breakfast, and if taken at lunch, it will not be repeated and taken tomorrow.

Thanks to all.

What I have tried:

VB
Dim conn As SqlConnection = New SqlConnection_
("Data Source=DESKTOP-NO2IM8B\SQLEXPRESS;_
Initial Catalog=Barcode;User ID=sa;Password=P@ssw0rd")
        conn.Open()
        Dim cmd2 As SqlCommand = New SqlCommand_
        ("SELECT sale_id FROM Table_1 WHERE _
          Meal_time = @Meal_time AND sale_id = @sale_id ", conn)
        cmd2.Parameters.AddWithValue("Meal_time", DropDownList2.Text)
        cmd2.Parameters.AddWithValue("@sale_id", txtUserName.Text)
        'cmd2.Parameters.AddWithValue("@date", Now)
        Dim myreader As SqlDataReader = cmd2.ExecuteReader()
        If myreader.Read() Then
            conn.Close()
            'Label12.text = "The student already exists+الطالب موجود بالفعل"
            Dim message As String = _
                        "الطالب موجود بالفعل+The student already exists"
            Dim sb As New System.Text.StringBuilder()
            sb.Append("")
            sb.Append("window.onload=function(){")
            sb.Append("alert('")
            sb.Append(message)
            sb.Append("')};")
            sb.Append("")
            ClientScript.RegisterClientScriptBlock_
                         (Me.GetType(), "alert", sb.ToString())

        Else
            conn.Close()

            Dim cmd As SqlCommand = New SqlCommand_
              ("INSERT INTO Table_1(sale_id,building,Meal_time,date) _
                VALUES(@sale_id,@building,@Meal_time,@date)", conn)
            cmd.Parameters.AddWithValue("sale_id", txtUserName.Text)
            cmd.Parameters.AddWithValue("building", DropDownList1.Text)
            cmd.Parameters.AddWithValue("Meal_time", DropDownList2.Text)
            cmd.Parameters.AddWithValue("date", Now)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

            'Label14.Text = ("تم الادخال")
            Dim message As String = "تم الادخال+_
                        The student number has been entered"
            Dim sb As New System.Text.StringBuilder()
            sb.Append("")
            sb.Append("window.onload=function(){")
            sb.Append("alert('")
            sb.Append(message)
            sb.Append("')};")
            sb.Append("")
            ClientScript.RegisterClientScriptBlock_
                         (Me.GetType(), "alert", sb.ToString())
        End If
    End Sub 
Posted
Updated 5-Nov-23 0:36am
v3
Comments
CHill60 30-Oct-23 5:37am    
Stop posting the same question over and over again. Update your original post

1 solution

If possible, change the table so that you have a unique constraint on the field. Let the database engine take care of the hard work for you.
 
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