Results 1 to 12 of 12

Thread: Using an sql statement in my datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Using an sql statement in my datagridview

    I have a datagridview and a sql statement and I need to get the data to populate in the datagridview.

    Query:
    sqlStr = "SELECT Release,PRODWK,SHIPMON,COMPL,TGTSHIP,SCHPROD,AB,ARM,SHAFT,FIT,HDW,FIN,WELDCMP,DATE_REQ"
    sqlStr += " FROM jobscopeMeyer.dbo.PPJOBM p "
    sqlStr += " Left JOIN ("
    sqlStr += " Select SearchKey As RELEASE_PU, "
    sqlStr += " MAX(Case When DataItem = '27 PRODWK' THEN AlphaValue END) AS 'PRODWK',"
    sqlStr += " MAX(CASE WHEN DataItem = '28 SHIPMON' THEN AlphaValue END) AS 'SHIPMON',"
    sqlStr += " MAX(CASE WHEN DataItem = '30 %COMPL' THEN AlphaValue END) AS 'COMPL',"
    sqlStr += " MAX(CASE WHEN DataItem = '31 TGTSHIP' THEN AlphaValue END) AS 'TGTSHIP',"
    sqlStr += " MAX(CASE WHEN DataItem = '70 SCHPROD' THEN AlphaValue END) AS 'SCHPROD',"
    sqlStr += " MAX(CASE WHEN DataItem = '81 AB%' THEN NumericValue END) AS 'AB',"
    sqlStr += " MAX(CASE WHEN DataItem = '82 ARM%' THEN NumericValue END) AS 'ARM',"
    sqlStr += " MAX(CASE WHEN DataItem = '83 SHAFT%' THEN NumericValue END) AS 'SHAFT',"
    sqlStr += " MAX(CASE WHEN DataItem = '84 FIT%' THEN NumericValue END) AS 'FIT',"
    sqlStr += " MAX(CASE WHEN DataItem = '85 HDW%' THEN NumericValue END) AS 'HDW',"
    sqlStr += " MAX(CASE WHEN DataItem = '86 FIN%' THEN NumericValue END) AS 'FIN',"
    sqlStr += " MAX(CASE WHEN DataItem = '87 WELDCMP' THEN AlphaValue END) AS 'WELDCMP'"
    sqlStr += " From jobscopeMeyer.dbo.UserDefinedFields"
    sqlStr += " GROUP BY SearchKey"
    sqlStr += ") PU ON RELEASE_PU = RELEASE"
    sqlStr += " Left JOIN ("
    sqlStr += " Select RELEASE AS RELEASE_PJ, MAX(DATE_REQUESTED) AS DATE_REQ"
    sqlStr += " From JobscopeMeyer.dbo.PPJOBD"
    sqlStr += " WHERE DATE_REQUESTED NOT IN (0,99999999)"
    sqlStr += " GROUP BY RELEASE"
    sqlStr += " ) PJ ON RELEASE_PJ = RELEASE"
    sqlStr += " where RELEASE = '" + strRelease + "'"

    Here is the output from the query:
    Name:  Database Output.jpg
Views: 1212
Size:  5.4 KB


    Here is what the Datagridview look like:
    Name:  JobScreen.jpg
Views: 1446
Size:  5.7 KB

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

    Re: Using an sql statement in my datagridview

    What is the problem? Do you have a question?

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Re: Using an sql statement in my datagridview

    The problem I have is how do I put the data from the query in the Datagridview?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Using an sql statement in my datagridview

    What type of database is it?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Using an sql statement in my datagridview

    You need to post your code that runs the query if you have any. If you don’t, any advice depends on the type of database. It’s just a case of filling a DataTable, then binding to your dgv. Slightly more complex coding as you have added columns to your dgv at design time…

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

    Re: Using an sql statement in my datagridview

    Populate a DataTable and bind it to the grid, preferably via a BindingSource. You can use a data adapter and call Fill on it or you can use a data reader and call Load on the DataTable. There are loads of examples around, which you should have been able to find for yourself with a simple web search. Now you're here, try following the CodeBank link in my signature below and check out my own examples in my thread on Retrieving & Saving Data.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using an sql statement in my datagridview

    You should also clean up that code to create the SQL. There's no reason to use all that string concatenation. Recent versions of VB support multiline String literals, e.g.
    vb.net Code:
    1. Dim sql = "SELECT Column1
    2.                  ,Column2
    3.                  ,Column3
    4.            FROM MyTable"
    In earlier versions, you could use an XML literal, e.g.
    vb.net Code:
    1. Dim sql = <sql>SELECT Column1
    2.                      ,Column2
    3.                      ,Column3
    4.                FROM MyTable</sql>.Value
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Re: Using an sql statement in my datagridview

    Quote Originally Posted by .paul. View Post
    what type of database is it?
    mssql

  9. #9

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

    Re: Using an sql statement in my datagridview

    It really doesn't matter what the database is. The principle is exactly the same regardless. All that changes is the specific ADO.NET provider, the connection string and possibly the SQL syntax. You still call Fill on a data adapter or Load on a DataTable with a data reader regardless. I haven't yet but I'm willing to bet that if I do a web search with some obvious keywords, I'll find examples of exactly that.

    ...

    Sure enough, I just searched the web for "vb.net populate datagridview from database" and the first two results included code examples of using a data adapter. It's not hard to find this stuff so any effort at all should yield results. If you have issues implementing what you find then that's something to post a question about, but at least look for yourself first.
    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

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Using an sql statement in my datagridview

    Quote Originally Posted by jmcilhinney View Post
    It really doesn't matter what the database is. The principle is exactly the same regardless. All that changes is the specific ADO.NET provider, the connection string and possibly the SQL syntax.
    A few questions about the database reveal whether it’s going to be OLEDB, SQLDB, or even MySQLDB. But as you say, all of the methods are the same, but I might’ve saved us some work later

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

    Re: Using an sql statement in my datagridview

    Quote Originally Posted by .paul. View Post
    as you say, all of the methods are the same
    It might be an OleDbDataAdapter or a SqlDataAdapter or a MySqlDataAdapter but it's still a data adapter and you use them all in exactly the same way, so someone should be able to find out for themselves that you use a data adapter and then ask specific questions if required. If you don't even know the generalities of ADO.NET then you haven't actually tried to work out the problem for yourself, but even more so, you haven't tried to learn a topic beforehand so that you don't encounter problems in the first place. In my "humble" opinion, sites like this one should be used to help with the stuff you can't work out for yourself, not the stuff you can't be bothered to work out. That type of approach never really helps anyone in anything but the shortest of terms. If you want to write software then you should be able to use a search engine. The search keywords I used in post #10 were all taken from the OP's own post #1. If you can type those words here, you can type them in a search engine and find a lot of the information for yourself. That said, working through a tutorial or two on data access before even attempting to write data access code is what the conscientious would do, so such a search would not be required.
    Quote Originally Posted by .paul. View Post
    I might’ve saved us some work later
    That later work is really all that should have been needed, unless a more specific issue was encountered.
    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