Results 1 to 8 of 8

Thread: VB and SQL

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    18

    VB and SQL

    Okay, I've got some other college work I might need some help with. I have a major project coming up in class next Wednesday that has to do with me creating a Windows Form-based application that connects to a database through SQL to allow for a user to connect using a login and password, also stored on a table within the database, and can allow for said user to edit, update, and change entries on another table.

    Reason I'm asking for help is for one of four main things: For starters, when I installed SQL, after installing Visual Studio, I'm getting a connection issue when trying to add a data connection in the Server Explorer. Can anyone help me understand what exactly I'm doing wrong?

    Second, once I have the tables created, the Windows forms must display said table and allow for editing. Is there anything I should know about allowing a user to do so?

    Third, I do understand that, to get the form's variables and such to be recognized by SQL, a @ is needed before each variable entry in the SQL coding, if that makes any sense. This in mind, how do I get the user's input from another form I've made for the user to input their user name and password to the main form to be used in SQL? I've got an idea behind that, but I figured I'd ask.

    Lastly, there is a requirement to allow for a user to switch tables within the database, however the project doesn't mention if it's required to keep it in the same form. Assuming it is required, how would I do that?

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

    Re: VB and SQL

    Here's a good place to start. http://msdn.microsoft.com/en-US/vstu...#formsoverdata
    There's lots of information in this forum on how to transfer values between forms, just do a search.

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

    Re: VB and SQL

    Quote Originally Posted by NFSRacer View Post
    Reason I'm asking for help is for one of four main things: For starters, when I installed SQL, after installing Visual Studio, I'm getting a connection issue when trying to add a data connection in the Server Explorer. Can anyone help me understand what exactly I'm doing wrong?
    We'd have to know what you're doing and what happens when you do it to know what you're doing wrong.
    Quote Originally Posted by NFSRacer View Post
    Second, once I have the tables created, the Windows forms must display said table and allow for editing. Is there anything I should know about allowing a user to do so?
    If you want to display a table of data then you'll probably want to use a DataGridView control. You can bind a populated DataTable to the grid by setting the control's DataSource property. Ideally you would bind the DataTable to a BindingSource and bind that to the grid.
    Quote Originally Posted by NFSRacer View Post
    Third, I do understand that, to get the form's variables and such to be recognized by SQL, a @ is needed before each variable entry in the SQL coding, if that makes any sense. This in mind, how do I get the user's input from another form I've made for the user to input their user name and password to the main form to be used in SQL? I've got an idea behind that, but I figured I'd ask.
    Follow the CodeBank link in my signature below and check out my WinForms Login thread. You might also like to follow the Blog link and check out my post on Parameters In ADO.NET for more info on the @ thing. There's also a series of blog posts on moving data between forms.
    Quote Originally Posted by NFSRacer View Post
    Lastly, there is a requirement to allow for a user to switch tables within the database, however the project doesn't mention if it's required to keep it in the same form. Assuming it is required, how would I do that?
    When you bind data to a DataGridView, it will automatically create columns by default. To switch tables you can unbind the old table, clear the columns and then bind the new table. If there's a BindingSource then you must unbind it from the grid. The columns are created when you set the DataSource property of the grid.
    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    18

    Re: VB and SQL

    Okay, so the scope of my problem is way bigger than I thought. How exactly do I go about trying to match login information -- that is, username and password -- to that of what's already in an SQL table? Or, in a better explanation, how do I take the username and password in two VB text boxes and compare it to those inside an SQL table to make it almost like logging into a website?...That might have just been a reiteration, but I'm EXTREMELY pressed for time for this project, and need to have it done just about yesterday...

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

    Re: VB and SQL

    I would create a connection object, then create a command object from that, then write a query that looked for the user name and password. It would look something like this, though I don't know enough (and don't want to know enough) to fully complete it:

    Code:
    Using cn as New SqlClient.SqlConnection(yourConnectionStringHere)
     Using cmd as SqlClient.SqlCommand = cn.CreateCommand
      Try
        cn.Open
        cmd.CommandText = "SELECT someFieldLikeThePrimaryKey FROM tableWithUsernamesAndPasswords WHERE Username = @p1 AND Password = @p2"
        cmd.Parameters.AddWithValue("@p1",textboxWithUserName.Text)
        cmd.Parameters.AddWithValue("@p2",textboxWithPassword.Text)
        If cmd.ExecuteScalar Is Nothing Then
         'It wasn't valid.
        Else
         'It was valid.
       End If
      Catch ex As Exception
        'Do something here.
      End Try
     End Using
    End Using
    I'd probably have this in a method that returned a Boolean, so if it was valid I'd return True and if it was not valid I'd return False.
    My usual boring signature: Nothing

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

    Re: VB and SQL

    Quote Originally Posted by NFSRacer View Post
    Okay, so the scope of my problem is way bigger than I thought. How exactly do I go about trying to match login information -- that is, username and password -- to that of what's already in an SQL table? Or, in a better explanation, how do I take the username and password in two VB text boxes and compare it to those inside an SQL table to make it almost like logging into a website?...That might have just been a reiteration, but I'm EXTREMELY pressed for time for this project, and need to have it done just about yesterday...
    You would do what I already told you to do and read my CodeBank thread on the subject. We post threads in the CodeBank specifically so we don't have to keep providing the same information for the same questions over and over. I'm not fond of reiteration myself so you probably ought to read the answer provided and follow the advice it contains.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    18

    Re: VB and SQL

    Quote Originally Posted by jmcilhinney View Post
    You would do what I already told you to do and read my CodeBank thread on the subject. We post threads in the CodeBank specifically so we don't have to keep providing the same information for the same questions over and over. I'm not fond of reiteration myself so you probably ought to read the answer provided and follow the advice it contains.
    Well, I know you keep mentioning that, but having my questions directly answered, instead of having to sift through data, saves me more time than you think. As it stands, I'm running into more roadblocks.

    For starters, I'm now trying to figure out how to have a ComboBox load in its collection from a database's table, without inserting, updating, or deleting any entries. How exactly do I do that?

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

    Re: VB and SQL

    Oh, I see. I didn't realise that your time was so much more important than mine. I thought that I could put examples in one thread and then everyone who needed those examples could just look there but now I see that it would be far more trouble for you to click that link and spend a couple of minutes reading than it would be for me to keep providing the same information over and over again, hundreds of times. How selfish of me. Please accept my apologies.

    As for your new question, follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data, which includes an example of retrieving data that doesn't need to be updated. I probably should have directed you to that thread in the first place because, if you'd already read it, then you wouldn't even need to ask that last question because you'd already know.
    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

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