Page 2 of 2 FirstFirst 12
Results 41 to 51 of 51

Thread: Textbox and SQL Countdown

  1. #41

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by jmcilhinney View Post
    It doesn't "stand for" anything. It's a Using statement. It creates a variable scope and disposes the object assigned to it at the end of the block.

    Note that I simply searched the web for "vb.net using" and that link I provided was the first result. You can find this information for yourself if you care to look. Ask schoemr. She'll tell you.
    I did, but I did not type in the search box correctly. When I say I'm new to VB, this is the first two months I've ever worked on it. Its a learning curve for me for sure. I appreciate the team here helping and guiding.

  2. #42
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Textbox and SQL Countdown

    wes4dbt,

    So do you think the OP is going to plug the code into his button, press it and boom! his life's mission is complete? There certainly are times where I wished someone can just shoot me a piece of code so that I can get on with it, but I am trying to teach him something here. I'm not sure what your problem is but after the OP started this thread, and obviously got some responses, he contacted me privately to ask for help as I have been of some assistance to him in another thread. Not everyone has been in this game for a zillion years.

    You cannot tell me that directly accessing the database with code is a better option than doing it with a dataset. At the end of the day that is a developer's preference. Both methods have advantages as well as disadvantages.

    For a dataset there are several advantages such as easier databinding to controls, easier to use, simplified CRUD operations, features for enforcing data consistency and integrity, etc, etc. while it is also true that for large and complex applications direct access may be more advisable.

  3. #43
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Textbox and SQL Countdown

    newVBM3,

    I'm happy that you managed to add your dataset to your project. You will see that working with a dataset can simplify many things.

    Can you confirm that if you expand the dataset you can see your table? (I usually when I add a dataset select all the tables not only one)

    Next, on a Userfrom add a combobox from the toolbox. You will see there is a small arrow symbol

    Attachment 187089

    Click on it, check "Use Data Bound Items" and configure it as follows:

    In the top dropdown (Data Source) Expand "Other Data Sources" >> "Project Data Sources" >> 'your database name' >> 'your table name'

    Display Member: your S/N column

    Value Member: your S/N column

    Selected Value: you can leave blank for now.

    Attachment 187090

    Once you have done that and you run the application you will see the dropdown is populated with the data of your S/N column. I know you said that you will use a barcode scanner to get the values, we will get to that. Can you confirm that your combobox is populating?

    You will also notice that your DataSet, BindingSource and TableAdapter was added automatically below in the component tray by the designer. It is important that you take note of that as you will need it.

    Attachment 187091
    Last edited by schoemr; Mar 1st, 2023 at 02:06 AM.

  4. #44
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Textbox and SQL Countdown

    Quote Originally Posted by schoemr View Post
    wes4dbt,

    So do you think the OP is going to plug the code into his button, press it and boom! his life's mission is complete? There certainly are times where I wished someone can just shoot me a piece of code so that I can get on with it, but I am trying to teach him something here. I'm not sure what your problem is but after the OP started this thread, and obviously got some responses, he contacted me privately to ask for help as I have been of some assistance to him in another thread. Not everyone has been in this game for a zillion years.

    You cannot tell me that directly accessing the database with code is a better option than doing it with a dataset. At the end of the day that is a developer's preference. Both methods have advantages as well as disadvantages.

    For a dataset there are several advantages such as easier databinding to controls, easier to use, simplified CRUD operations, features for enforcing data consistency and integrity, etc, etc. while it is also true that for large and complex applications direct access may be more advisable.
    For some reason lately you seem to be working very hard at being unpleasant.

    I never implied that dday's post would solve all the OP's future problems. But it does answer the question that was asked and there was no response from the OP indicating if that code was sufficient for his needs or not. I have no idea what else the OP is intending to do in the future. Nor did I make any claims about if directly assessing the database is better or worse.

  5. #45
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Textbox and SQL Countdown

    No wes, I wasn't trying to be unpleasant. You perceive it that way. But you know I'm very observant. I picked up many things here regarding many people. I can tell you who post as multiple people etc. I also know exactly where your 'sentiments' started (in another section of vbforums) I now also know why.

    I'm sorry you feel that lately I'm unpleasant...

  6. #46

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Thank you schoemr, I appreciate all of your feedback. I do plan on using VB for the foreseeable future.

  7. #47

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Hi all. I finished my project. Thanks schoemr and dday.

    Here is example of how I did it:
    Code:
    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox_TextChanged
    Dim con As New SqlConnection("Data Source=SERVER NAME; Initial Catalog=DATABASE NAME; Integrated Security = True)
    con.Open()
    Dim str As String
    str = "SELECT Crayon_ID FROM Crayon_Lot WHERE Crayon_SN = ' " + TextBox.Text + "'"
    Dim cmd As New SqlCommand(str,con)
    rdr = cmd.ExecuteReader
    If rdr.Read() Then
    Label.Text = rdr(Crayon_ID).ToString()
    End If
    
    If "Crayon_SN" Then
    Label. Text. ToString(Crayon_ID")
    End If
    
    If Label.Text = "Crayon_ID" Then
        countdown
    End If
    con.Close()
    End Sub
    Last edited by newVBM3; Mar 2nd, 2023 at 11:39 PM.

  8. #48
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Textbox and SQL Countdown

    That is OK in principle but there are a number of things to address there. The most glaring is that you are using string concatenation to insert values into SQL code. That is an issue for various reasons but the most important is the potential for SQL injection attacks. A malicious user could potentially corrupt or delete your entire database by entering the right SQL code into your TextBox. The proper way to handle this is by using parameters. There's plenty of information around about that in various places but here is my personal take on the subject.

    Secondly, you are creating objects and opening connections without closing or disposing them. That means that your app will be holding onto system resources for no reason. Any time you create an object that can be disposed, you should always dispose it when you're done with it. Any time you open a connection, you should always close it when you're done with it. The best way to do both is usually with a Using block. The Using statement will declare the variable and create the object, then the object will be closed/disposed at the end of the block. ALWAYS use a Using statement to create a disposable object unless you need it to live beyond the scope in which you create it.

    A less significant issue is that you are using a control as data storage. Controls are for displaying data and user interaction. They are not for data storage. There's never a reason for you to test what the Text of a Label contains. Whatever is in a Label is there because you put it there from somewhere else. If you need to know what that value is, you should be looking at the original source.

    Finally, the fact that you're querying the database on the TextChanged event is an issue. From what you posted earlier, it seems that you are expecting the user to enter 8 characters. That means that you will be querying the database 8 times and 7 of them will be pointless. You would also query the database again if the user cleared the TextBox, which be pointless again. If valid input values will always be the same length then you should add a condition to only query if the input is that length. If you don't know what length the input should be, you can start/restart a Timer on the TextChanged event and then query the database when the Timer Ticks, if and only if the TextBox contains text. You can set the Interval to say 300-500 milliseconds so that the user can type multiple characters in a row without Tick events in between but still won't have to wait long for the query to occur after they finish typing.

    With all that in mind, your code might look like the following:
    vb.net Code:
    1. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    2.     Dim serialNumber = TextBox1.Text
    3.  
    4.     If serialNumber.Length = 8 Then
    5.         Dim connectionString = "Data Source=SERVER NAME; Initial Catalog=DATABASE NAME; Integrated Security = True"
    6.         Dim sql = "SELECT Crayon_ID FROM Crayon_Lot WHERE Crayon_SN = @Crayon_SN"
    7.  
    8.         Using connection As New SqlConnection(connectionString),
    9.               command As New SqlCommand(sql, connection)
    10.             command.Parameters.Add("@Crayon_SN", SqlDbType.VarChar, 50).Value = serialNumber
    11.  
    12.             connection.Open()
    13.  
    14.             Using reader = command.ExecuteReader()
    15.                 If reader.Read() Then
    16.                     Label1.Text = reader.GetString(0)
    17.                 Else
    18.                     'Start countdown or whatever.
    19.                 End If
    20.             End Using
    21.         End Using
    22.     End If
    23. End Sub
    or:
    vb.net Code:
    1. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    2.     Timer1.Stop()
    3.     Timer1.Start()
    4. End Sub
    5.  
    6. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    7.     Timer1.Stop()
    8.  
    9.     Dim serialNumber = TextBox1.Text
    10.  
    11.     If serialNumber.Length > 0 Then
    12.         Dim connectionString = "Data Source=SERVER NAME; Initial Catalog=DATABASE NAME; Integrated Security = True"
    13.         Dim sql = "SELECT Crayon_ID FROM Crayon_Lot WHERE Crayon_SN = @Crayon_SN"
    14.  
    15.         Using connection As New SqlConnection(connectionString),
    16.               command As New SqlCommand(sql, connection)
    17.             command.Parameters.Add("@Crayon_SN", SqlDbType.VarChar, 50).Value = serialNumber
    18.  
    19.             connection.Open()
    20.  
    21.             Using reader = command.ExecuteReader()
    22.                 If reader.Read() Then
    23.                     Label1.Text = reader.GetString(0)
    24.                 Else
    25.                     'Start countdown or whatever.
    26.                 End If
    27.             End Using
    28.         End Using
    29.     End If
    30. End Sub
    Last edited by jmcilhinney; Mar 2nd, 2023 at 11:44 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #49
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    Code:
    If rdr.Read() Then
    Label.Text = rdr(Crayon_ID).ToString()
    End If
    
    If Label.Text = "@ID" Then
        countdown
    End If
    Why would the Label ever contain "@ID"? Are you expecting that value to come from the database or is that the default value you put there that would be replaced if a match is found? If a match is not found, will the Label always contain "@ID" or will it possibly contain the ID you got from a previous query?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #50

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by jmcilhinney View Post
    Why would the Label ever contain "@ID"? Are you expecting that value to come from the database or is that the default value you put there that would be replaced if a match is found? If a match is not found, will the Label always contain "@ID" or will it possibly contain the ID you got from a previous query?
    I will update my code above. I looked and forgot to add the close connection and some other stuff to my sample code. I meant to put crayon ID. The s/n is scanned to textbox 1, search through that specific table and colum, if s/n found and correct ID, put grade ID from that column and row into the label, if label string is correct, go ahead begin countdown. I want it where I'm only pulling/view data from DB. Not updating anything.
    Last edited by newVBM3; Mar 2nd, 2023 at 11:57 PM.

  11. #51

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by jmcilhinney View Post
    That is OK in principle but there are a number of things to address there. The most glaring is that you are using string concatenation to insert values into SQL code. That is an issue for various reasons but the most important is the potential for SQL injection attacks. A malicious user could potentially corrupt or delete your entire database by entering the right SQL code into your TextBox. The proper way to handle this is by using parameters. There's plenty of information around about that in various places but here is my personal take on the subject.

    Secondly, you are creating objects and opening connections without closing or disposing them. That means that your app will be holding onto system resources for no reason. Any time you create an object that can be disposed, you should always dispose it when you're done with it. Any time you open a connection, you should always close it when you're done with it. The best way to do both is usually with a Using block. The Using statement will declare the variable and create the object, then the object will be closed/disposed at the end of the block. ALWAYS use a Using statement to create a disposable object unless you need it to live beyond the scope in which you create it.

    A less significant issue is that you are using a control as data storage. Controls are for displaying data and user interaction. They are not for data storage. There's never a reason for you to test what the Text of a Label contains. Whatever is in a Label is there because you put it there from somewhere else. If you need to know what that value is, you should be looking at the original source.

    Finally, the fact that you're querying the database on the TextChanged event is an issue. From what you posted earlier, it seems that you are expecting the user to enter 8 characters. That means that you will be querying the database 8 times and 7 of them will be pointless. You would also query the database again if the user cleared the TextBox, which be pointless again. If valid input values will always be the same length then you should add a condition to only query if the input is that length. If you don't know what length the input should be, you can start/restart a Timer on the TextChanged event and then query the database when the Timer Ticks, if and only if the TextBox contains text. You can set the Interval to say 300-500 milliseconds so that the user can type multiple characters in a row without Tick events in between but still won't have to wait long for the query to occur after they finish typing.

    With all that in mind, your code might look like the following:
    vb.net Code:
    1. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    2.     Dim serialNumber = TextBox1.Text
    3.  
    4.     If serialNumber.Length = 8 Then
    5.         Dim connectionString = "Data Source=SERVER NAME; Initial Catalog=DATABASE NAME; Integrated Security = True"
    6.         Dim sql = "SELECT Crayon_ID FROM Crayon_Lot WHERE Crayon_SN = @Crayon_SN"
    7.  
    8.         Using connection As New SqlConnection(connectionString),
    9.               command As New SqlCommand(sql, connection)
    10.             command.Parameters.Add("@Crayon_SN", SqlDbType.VarChar, 50).Value = serialNumber
    11.  
    12.             connection.Open()
    13.  
    14.             Using reader = command.ExecuteReader()
    15.                 If reader.Read() Then
    16.                     Label1.Text = reader.GetString(0)
    17.                 Else
    18.                     'Start countdown or whatever.
    19.                 End If
    20.             End Using
    21.         End Using
    22.     End If
    23. End Sub
    or:
    vb.net Code:
    1. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    2.     Timer1.Stop()
    3.     Timer1.Start()
    4. End Sub
    5.  
    6. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    7.     Timer1.Stop()
    8.  
    9.     Dim serialNumber = TextBox1.Text
    10.  
    11.     If serialNumber.Length > 0 Then
    12.         Dim connectionString = "Data Source=SERVER NAME; Initial Catalog=DATABASE NAME; Integrated Security = True"
    13.         Dim sql = "SELECT Crayon_ID FROM Crayon_Lot WHERE Crayon_SN = @Crayon_SN"
    14.  
    15.         Using connection As New SqlConnection(connectionString),
    16.               command As New SqlCommand(sql, connection)
    17.             command.Parameters.Add("@Crayon_SN", SqlDbType.VarChar, 50).Value = serialNumber
    18.  
    19.             connection.Open()
    20.  
    21.             Using reader = command.ExecuteReader()
    22.                 If reader.Read() Then
    23.                     Label1.Text = reader.GetString(0)
    24.                 Else
    25.                     'Start countdown or whatever.
    26.                 End If
    27.             End Using
    28.         End Using
    29.     End If
    30. End Sub
    Thank you. I will update my code tomorrow and come back here to update.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width