Page 1 of 2 12 LastLast
Results 1 to 40 of 51

Thread: Textbox and SQL Countdown

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Textbox and SQL Countdown

    Hello.

    I have a countdown timer working where you manually enter "ABC". I use a "If End If" statement. Works perfectly like this.

    What I am trying to figure out now is how to connect it to SQL Databse where a Table already exists. For Example, I want to scan S/N "ABC12345", and I want my countdown to look in the database for the associated color, and start countdown.

    Here is summary:
    1. Enter S/N in textbox "ABC12345" entered
    2. "#ADD8E6" is the color associated with this S/N
    3. Therefore, a countdown starts for 1 hour.

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

    Re: Textbox and SQL Countdown

    So you're asking how to query a database with a filter?
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Now that I think about it, yes. This is my first attempt at trying to connecting to SQL Database.
    Here is my sample:

    sqlstmt = "SELECT Color_ID FROM Crayon_LOT WHERE Color_ID = #ADD8E6"

    Not sure how to use that in my If End If condition. My sql filter is probably not correct as well.

    Code:
    If Textbox1.ColorID = #ADD8E6" Then
        countdown = TimeSpan.FromSeconds(5)
                endTime = DateTime.Now.Add(countdown)
    End If
    Last edited by newVBM3; Feb 28th, 2023 at 10:45 AM.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Textbox and SQL Countdown

    A cursory glance at your SQL, it looks like you only want to verify that the value exists rather than use any actual value.

    What you can do is get the number of records using the T-SQL function COUNT function, use ExecuteScalar to get the single value, and then use your conditional statement to do... something.

    Here is an example:
    Code:
    Private Function ColorIdExists(colorId As String) As Boolean
        Dim connectionString = "Data Source=MyServerName;Initial Catalog=MyDatabaseName;User ID=MyUserName;Password=MySuperSecretPassword"
        Dim count = 0
    
        Try
            Using con = New SqlConnection(connectionString)
                Using cmd = New SqlCommand("SELECT Count(Color_ID) FROM Crayon_LOT WHERE Color_ID=@colorId;", con)
                    cmd.Parameters.AddWithValue("@colorId", colorId)
    
                    con.Open()
                    count = Convert.ToInt32(cmd.ExecuteScalar())
                    con.Close()
                End Using
            End Using
        Catch ex As Exception
            ' do something with the exception
        End Try
    
        Return count > 0
    End Function
    Usage:
    Code:
    If (ColorIdExists(TextBox1.Text)) Then
        ' it exists!
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    I want to verify that that specific S/N is indeed that Color_ID. And if it is that Color_ID, go ahead and start counting down.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Textbox and SQL Countdown

    Then there you go, that example should do exactly that.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    I want to verify that that specific S/N is indeed that Color_ID. And if it is that Color_ID, go ahead and start counting down.
    In your example the "input" is "ABC12345" and the "output" is "#ADD8E6". Based on that, I would imagine you have at least two different columns in your database; one for what you call the "S/N", and one for the associated color.

    From that, I would imagine that the SQL SELECT would need to be along the lines of:

    Code:
    SELECT Color_ID FROM Crayon_LOT WHERE [S/N] = UserInput
    And you replace [S/N] with the actual column name in your database that stores the "S/N", and you replace UserInput with either the dangerous method of simple string concatenation of the input directly from the user, or the safer method of using query parameters.

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

    Re: Textbox and SQL Countdown

    Hi, are you asking how to connect to and query a database?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    That is correct, I have multiple columns. I think I should look for the Color_ID, as that is when I want to start a specific countdown. The table is called Crayon_LOT. The two columns I am interested in is Crayon_SN and Color_ID.

    Like this?:
    Code:
    SELECT Color_ID FROM Crayon_LOT WHERE Crayon_SN  = UserInput
    What are parameters I can use for UserInput?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Hello. Yes.

    I have a Label1. 'Scan Crayon Label'. Textbox1 to receive input.

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

    Re: Textbox and SQL Countdown

    Okay no problem, do want to query inside the database itself or are you using Visual Studio?

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

    Re: Textbox and SQL Countdown

    The first thing you have to do is to add a DataSource to your project. Did you do that already or should I show you?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Using Visual Studio.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    I have.
    Code:
    Public Const CR_Connect "Data Source=MyServerName;Initial Catalog=MyDatabaseName;User ID=MyUserName;Password=MySuperSecretPassword"
    Is this correct way?

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

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    I have.
    Code:
    Public Const CR_Connect "Data Source=MyServerName;Initial Catalog=MyDatabaseName;User ID=MyUserName;Password=MySuperSecretPassword"
    Is this correct way?
    Okay so it is a SQL database. But I don't think that the name of the database is "MyDatabaseName"??

    In VS you have to add a datasouce. Do you have SQL Server Management Studio (SSMS) installed? That would be the easiest I think to get the database properties.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Yes it is a SQL Database. Yes I have SSMS installed. No, database is not actually "MyDatabaseName". I used that as a generic name for this thread.

    Adding through Server Explorer and then right clicking on Data Connections --> Add Connection?

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

    Re: Textbox and SQL Countdown

    In VS click on View >> Other Windows >> Data Sources

    You should see this as per image.

    Click on Add datasource as in the image.

    It will take you through the process of adding the datasouce. If you login to SSMS before you login you will see something like: MyServer\MyInstance.

    Attachment 187080


    You will need this to add your DataSource. This is where you specify and test the connection to the database. Once you have added the datasouce successfully, the hard part is over. Then we can work on you query.

    Edit: Assuming you have already attached your database in SSMS.
    Last edited by schoemr; Feb 28th, 2023 at 02:35 PM.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Attachment 187081

    Test Connect Succeeded. Yes, Database already attached to SSMS.

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

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    Attachment 187081

    Test Connect Succeeded. Yes, Database already attached to SSMS.
    The attachment thingy on vbforums is not working, you have to go down and add it though "Manage Attachments"

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    See new attachment.
    Attached Images Attached Images  

  21. #21
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Textbox and SQL Countdown

    Why are you using a textbox for this? A textbox allows people to type something in. In this case, the text may or may not match something in a database, so you are asking people to take a guess at which string is right, including the capitalization. If you are trying to frustrate the user, that seems like a good approach, but otherwise how about a listbox or drop down that includes just the options that are available?
    My usual boring signature: Nothing

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

    Re: Textbox and SQL Countdown

    Did you go though the rest of the prompts? It will ask you to check tables and also specify a dataset name (I usually just go with the default name). Once you have done that you should see your dataset in the datasources window.

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

    Re: Textbox and SQL Countdown

    Quote Originally Posted by Shaggy Hiker View Post
    Why are you using a textbox for this? A textbox allows people to type something in. In this case, the text may or may not match something in a database, so you are asking people to take a guess at which string is right, including the capitalization. If you are trying to frustrate the user, that seems like a good approach, but otherwise how about a listbox or drop down that includes just the options that are available?
    Yes Shaggy you are right But I think the OP first need to connect his database so that he can use it in Visual Studio. When we get to that part it is definitely something to implement.

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    I didn't get any other prompts after I successfully tested the connection.

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

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    I didn't get any other prompts after I successfully tested the connection.
    After clicking on test connection you should click NEXT, and NEXT and check "Tables" and Specify the dataset name..

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by schoemr View Post
    Yes Shaggy you are right But I think the OP first need to connect his database so that he can use it in Visual Studio. When we get to that part it is definitely something to implement.
    The reason why I did not go with a Dropdown or Listbox because those are premade options to choose from. And users will scan a barcode, where a crayon sn is always new and saved in sql. I have the textbox, because the user scans the barcode

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    The only option I got was Ok. I will do some searching.

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    All the tables are there in the dropdown arrow under the database. And my table of interest is there.

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

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    The only option I got was Ok. I will do some searching.
    Look at the 2nd image in post #17. You have to click "Add new datasource" - the wizard will then take you though the process of adding your dataset.

    If you did that you will be prompted to select "Tables" and also to specify the name of your dataset. Once you have added your dataset you will see it in the Data Sources window.

    If you select your dataset and click on "Edit DataSet with designer"

    Attachment 187083


    you will see your database. It will look something like this:

    Attachment 187084


    When you get here the rest is easy. I have to go now, but I will return in the morning and post more pictures if needed.

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

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    All the tables are there in the dropdown arrow under the database. And my table of interest is there.
    No one is going to know what you see in front of you unless you show us....

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Ok now I got the Data Source. I had to go to Views and have the option checked. Now It has the Data Sources tab next the Server Explorer and Toolbox. This time it did give the prompts, I walked through it and I selected the table from the database.

    I was mixing up Server Explorer with Data Source.

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by schoemr View Post
    Look at the 2nd image in post #17. You have to click "Add new datasource" - the wizard will then take you though the process of adding your dataset.

    If you did that you will be prompted to select "Tables" and also to specify the name of your dataset. Once you have added your dataset you will see it in the Data Sources window.

    If you select your dataset and click on "Edit DataSet with designer"

    Attachment 187083


    you will see your database. It will look something like this:

    Attachment 187084


    When you get here the rest is easy. I have to go now, but I will return in the morning and post more pictures if needed.
    Yes I made it this far. Ok ttyl.

  33. #33
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Textbox and SQL Countdown

    There really is no need to add a datasource to your project for this. Don't get me wrong, it could help, but it seems a bit overboard for what you're wanting to do.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  34. #34
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Textbox and SQL Countdown

    Quote Originally Posted by newVBM3 View Post
    The reason why I did not go with a Dropdown or Listbox because those are premade options to choose from. And users will scan a barcode, where a crayon sn is always new and saved in sql. I have the textbox, because the user scans the barcode
    Ah, so the user won't be typing this in? If so, then a label would be a better option. Of course, if the user will NORMALLY scan it, but OCCASIONALLY type it, then a textbox would be good.

    As a general rule, I don't go with the Datasource wizard. I use raw SQL as DDay showed earlier. Of course, that does require that you get your connection string right, which is easy to do after you have done it once.
    My usual boring signature: Nothing

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

    Re: Textbox and SQL Countdown

    Quote Originally Posted by dday9 View Post
    There really is no need to add a datasource to your project for this. Don't get me wrong, it could help, but it seems a bit overboard for what you're wanting to do.
    I was wonder the same thing. Your code in post #4 does what the OP asked for. Not sure why they're going through the process of adding a datasource.

  36. #36

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by dday9 View Post
    There really is no need to add a datasource to your project for this. Don't get me wrong, it could help, but it seems a bit overboard for what you're wanting to do.
    Okay I thought I called the data source properly with how you guided me. All new to me.

    I'm having trouble scanning a specific column.

    Here is how I want the app to function:
    SQL Server exists.
    1. User grabs crayon, scans the barcode, which is the crayons s/n
    2. If the crayon s/n is a specific crayon ID, then a countdown starts

    It will be for 7 different crayon ID colors. If I can get one I can pretty copy paste the rest to the If End if.

  37. #37

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by Shaggy Hiker View Post
    Ah, so the user won't be typing this in? If so, then a label would be a better option. Of course, if the user will NORMALLY scan it, but OCCASIONALLY type it, then a textbox would be good.

    As a general rule, I don't go with the Datasource wizard. I use raw SQL as DDay showed earlier. Of course, that does require that you get your connection string right, which is easy to do after you have done it once.
    Correct. User will be scanning crayon sn off a barcode. It will be scanned into the textbox. With occasionally typing it in.

    I am new, I am open to what is best practice for these kinds of applications.

    Get my connection string right?

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

    Re: Textbox and SQL Countdown

    DDay gave you an example in post #4. Did you try it? If so, what's the problem? What exactly are you having trouble with.

  39. #39

    Thread Starter
    Addicted Member
    Join Date
    Jan 2023
    Posts
    139

    Re: Textbox and SQL Countdown

    Quote Originally Posted by wes4dbt View Post
    DDay gave you an example in post #4. Did you try it? If so, what's the problem? What exactly are you having trouble with.
    What does "Using" stand for in the Try..Catch?

  40. #40
    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
    What does "Using" stand for in the Try..Catch?
    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.
    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

Page 1 of 2 12 LastLast

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