Visual Basic Coding Help: My code won't work at all
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Visual Basic Coding Help: My code won't work at all

  1. #1
    Join Date
    Oct 2013
    Posts
    10

    Visual Basic Coding Help: My code won't work at all

    I know I'm doing something wrong here, but my sleep deprived brain (had a minor family crises last night, had to stay up much longer than I normally do) just can't work it out.

    The allButton click event is suppose to show all the records in the database, the codeButton click even is supposed to show only the records with a code of PG24, and the nameButton click event is supposed to show only the record with Java in the MagName cell.

    Right now the only things working are the database and the exit button.

    I'd attach the database, but the file's invalid.

    Code:
    Option Explicit On
    Option Strict On
    Option Infer On
    
    Public Class MainForm
    
        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'MagazinesDataSet.tblMagazine' table. You can move, or remove it, as needed.
            Me.TblMagazineTableAdapter.Fill(Me.MagazinesDataSet.tblMagazine)
    
        End Sub
    
        Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
            Me.Close()
        End Sub
    
        Private Sub allButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles allButton.Click
            Dim records = From code In MagazinesDataSet.tblMagazine
            Select code
    
        End Sub
    
        Private Sub codeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles codeButton.Click
            Dim records = From code In MagazinesDataSet.tblMagazine
                          Where code.Code.ToUpper = "PG24"
                          Select code
    
        End Sub
    
        Private Sub nameButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles nameButton.Click
            Dim records = From magName In MagazinesDataSet.tblMagazine
                          Where magName.MagName.ToUpper = "Java"
                          Select magName
        End Sub
    End Class

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Visual Basic Coding Help: My code won't work at all

    In your "sleep deprived" state.. you've been trying to run Database queries in the VB IDE rather than passing them to the Database...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Oct 2013
    Posts
    10

    Re: Visual Basic Coding Help: My code won't work at all

    What does that mean?

  4. #4
    Join Date
    Oct 2013
    Posts
    10

    Re: Visual Basic Coding Help: My code won't work at all

    I'm sorry if this is against the rules, but due to lack of responses I'm bumping the thread.

  5. #5
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,823

    Re: Visual Basic Coding Help: My code won't work at all

    I'm not quite sure what you are trying to do but the code in all of those buttons looks like it would just be triggering compile errors

    To select records from a db you would use a query which needs to be enclosed in quotes and formed something like this

    "Select * from Table where Field=Value"
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Oct 2013
    Posts
    10

    Re: Visual Basic Coding Help: My code won't work at all

    I'm afraid your answer is causing more problems than it's solving.

  7. #7
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,823

    Re: Visual Basic Coding Help: My code won't work at all

    Perhaps you should do a search for working with data in VB.Net and/or sql queries
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Oct 2013
    Posts
    10

    Re: Visual Basic Coding Help: My code won't work at all

    Can't you guys help? I'm not asking you to do this for me, I just need help.

  9. #9
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,823

    Re: Visual Basic Coding Help: My code won't work at all

    Well I showed you how a select query should look and you said that confused you so I then suggested that you search on the web for some examples/info. From the looks of it you have never did anything with data in vb.net and the only line that is working is the one that was generated for you. all the code in the sub routines is not even close to valid.

    You need to have a look at how to query the database and how to filter your data, there is lots of info available on this if you just look for it.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Visual Basic Coding Help: My code won't work at all

    K here are two EXAMPLES...

    Using Stored procedures:
    Code:
    Dim sqlConn As New SqlConnection("YourConnectionString")
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    
    ActivitySelect = New Activity
    Try
    	With cmd 
    		sqlConn.Open()
    		.Connection = SQLConn
    		.CommandType = CommandType.StoredProcedure
    		.CommandText = "Activity_SelectByPrimaryKey"
    		.Parameters.AddWithValue("@Act_Id", Act_Id)
    		dr = .ExecuteReader
    	End With
    	If Not dr Is Nothing Then
    		If dr.HasRows Then
    			dr.Read()
    			With ActivitySelect
    				.Act_Id = dr("Act_Id")
    				.Activity = dr("Activity")
    			End With
    			dr.close()
    		End IF
    	End IF
    Catch ex As Exception
    	'ErrorLogging.ErrorLogInsert(ex.ToString, "ActivitySelect")
    Finally
    	sqlConn.Close()
    End Try
    And using SQL querys:
    Code:
    Dim sqlConn As New SqlConnection("YourConnectionString")
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    
    ActivitySelect = New Activity
    Try
    	With cmd 
    		sqlConn.Open()
    		.Connection = SQLConn
    		.CommandType = .CommandType = CommandType.Text
    		.CommandText = "Select * from Table where Field=Value"
    		dr = .ExecuteReader
    	End With
    	If Not dr Is Nothing Then
    		If dr.HasRows Then
    			dr.Read()
    			With TableSelect
    				.UId = dr("UId")
    				.Field= dr("Field")
    			End With
    			dr.close()
    		End IF
    	End IF
    Catch ex As Exception
    	'ErrorLogging.ErrorLogInsert(ex.ToString, "TableSelect")
    Finally
    	sqlConn.Close()
    End Try
    Last edited by GremlinSA; November 18th, 2013 at 12:58 AM.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center