Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used following code to show records in a datagridview but it shows nothing. I have tried to debug & when it step into GetData function following codes

VB
Catch ex As Exception
returnData = Nothing
If connection.State = ConnectionState.Open Then
    connection.Close()
End If


become gray & it shows error sign.
When I take my mouse pointer on it it shows message. It says "Cannot open database SUIMT requested by the login. The login failed. Login failed for user 'MY-PC\James'".

Please check my code & help me to get rid of this problem & don't forget to add example with your answer.

VB
Imports System.Data.SqlClient

Public Class Form34
    Private Const ConnectionString As String = "Server=.\SQLEXPRESS;" & _
    "Database=SUIMT;Trusted_Connection=True"


    
Public Function GetData() As DataTable
        Dim SelectQry = "Select row_num, stu_id, tot_amou, paid, du, aoins, due, dt From monthly_instal where stu_id = '" & cmbdmiidn.Text & "' AND dt = '" & cmbdmidt.Text & "'"
        Dim connection As New SqlConnection(ConnectionString)
        Dim returnData As New DataTable("monthly_instal")
        Try
            connection.Open()
            Dim command As New SqlCommand(SelectQry, connection)
            Dim adapter = New SqlDataAdapter(command)
            adapter.Fill(returnData)
            con.Close()
        Catch ex As Exception
            returnData = Nothing
            If connection.State = ConnectionState.Open Then
                connection.Close()
            End If
        End Try
        Return returnData
    End Function

    
Private Sub Form34_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbdmidt.Items.Clear()
        cmbdmidt.Text = "DATE"
        cmbdmiidn.Items.Clear()
        cmbdmiidn.Text = "ID NUMBER"
        txtdmirn.Text = "ROW NUMBER"
        con = New ADODB.Connection
        con.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SUIMT")
        
rst = New ADODB.Recordset
        With rst
            .Open("Select * From monthly_instal", con, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
            If .BOF = False Then
                .MoveFirst()
                While .EOF = False
                    If Not cmbdmidt.Items.Contains(.Fields("dt").Value) Then
                        cmbdmidt.Items.Add(.Fields("dt").Value)
                    End If
                    .MoveNext()
                End While
            End If
            .Close()
        End With
  
rst = New ADODB.Recordset
        With rst
            .Open("Select * From monthly_instal", con, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
            If .BOF = False Then
                .MoveFirst()
                While .EOF = False
                    If Not cmbdmiidn.Items.Contains(.Fields("stu_id").Value) Then
                        cmbdmiidn.Items.Add(.Fields("stu_id").Value)
                    End If
                    .MoveNext()
                End While
            End If
            .Close()
        End With

        Me.CREATEUSERToolStripMenuItem.Enabled = False
        Me.DELETEUSERToolStripMenuItem.Enabled = False
        Me.CHANGEPASSWORDToolStripMenuItem.Enabled = False
        Me.ASSIGNPERMISSIONToolStripMenuItem.Enabled = False
        Me.SHOWALLToolStripMenuItem.Enabled = False
        Me.CREATEACADEMICYEARToolStripMenuItem.Enabled = False
        Me.DELETEACADEMICYEARToolStripMenuItem.Enabled = False
        Me.CREATESESSIONToolStripMenuItem.Enabled = False
        Me.DELETESESSIONToolStripMenuItem.Enabled = False
        Me.CREATEDEPARTMENTToolStripMenuItem.Enabled = False
        Me.DELETEDEPARTMENTToolStripMenuItem.Enabled = False
        Me.CREATEEXAMTYPEToolStripMenuItem.Enabled = False
        Me.DELETEEXAMTYPEToolStripMenuItem.Enabled = False
        Me.CREATESUBJECTToolStripMenuItem.Enabled = False
        Me.DELETESUBJECTToolStripMenuItem.Enabled = False
        Me.ADMISSIONToolStripMenuItem.Enabled = False
        Me.DELETEADMISSIONToolStripMenuItem.Enabled = False
        Me.ADMISSIONREPORTToolStripMenuItem.Enabled = False
        Me.STUDENTINFORMATIONToolStripMenuItem.Enabled = False
        Me.DELETESTUDENTINFORMATIONToolStripMenuItem.Enabled = False
        Me.SEARCHSTUDENTINFORMATIONToolStripMenuItem.Enabled = False
        Me.STUDENTINFORMATIONREPORTToolStripMenuItem.Enabled = False
        Me.CREATESTUDENTRESULTToolStripMenuItem.Enabled = False
        Me.DELETESTUDENTRESULTToolStripMenuItem.Enabled = False
        Me.STUDENTRESULTREPORTToolStripMenuItem.Enabled = False
        Me.RESULTSHEETREPORTToolStripMenuItem.Enabled = False
        Me.CREATEMONTHLYINSTALLMENTToolStripMenuItem.Enabled = False
        Me.DELETEMONTHLYINSTALLMENTToolStripMenuItem.Enabled = False
        Me.MONTHLYINSTALLMENTREPORTToolStripMenuItem.Enabled = False
        Me.CREATEBALANCESHEETToolStripMenuItem.Enabled = False
        Me.DELETEBALANCESHEETToolStripMenuItem.Enabled = False
        Me.BALANCESHEETREPORTToolStripMenuItem.Enabled = False
        Me.ABOUTTHISPROGRAMToolStripMenuItem.Enabled = False
        Me.EXIToolStripMenuItem.Enabled = False
    End Sub

    
Private Sub cmbdmidt_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbdmidt.Leave
        dg1.DataSource = Nothing
        dg1.DataSource = GetData()


    End Sub

     
Private Sub butdmiclo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butdmiclo.Click
        speak34.Speak("Delete monthly installment window has been closed successfully")
        Me.Close()
    End Sub
End Class
Posted
Updated 12-Sep-10 23:02pm
v4
Comments
Kunal Chowdhury «IN» 13-Sep-10 5:02am    
Reason for Update: proper formatting of the code.
Maciej Los 8-Dec-10 14:47pm    
GetData() function use SQLConnection; Form_Load() function use ADODBConnection. You should decide which connection you want to use.
To understand difference see here: http://www.carlprothman.net/Default.aspx?tabid=86#SQLClientManagedProvider

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900