views:

105

answers:

2

Possible Duplicate:
Datagridview doesnt work!

I have written code to show records in a datagridview but it shows nothing. I have tried to debug & when it step into GetData function following codes

        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 & dont forget to add example with your answer.

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
A: 

Since, it seems from your post, you can connect during the load method, I'd say changing the ConnectionString constant at the top should be the first place to look.

The exception you're getting indicates that something is wrong with the database, the network, or how you're trying to connect. My experience is that the network and database are not usually the best place to start looking for issues when working with new code.

Brad Bruce
Please check it(http://stackoverflow.com/questions/3700650/datagridview-doesnt-work-in-vb-netit-shows-nothing) where I have written detail about my problem.
James
It shows "Cannot open database SUIMT requested by the login. The login failed. Login failed for user 'MY-PC\James'".
James
A: 

Looks to me like you are trying to use network credentials to connect to the database, but you are missing a part in the connection string.

Try changing:

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

To this:

Private Const ConnectionString As String = "Server=.\SQLEXPRESS;" & _
"Database=SUIMT;Trusted_Connection=True;Integrated Security=SSPI;"
Tony Abrams
I have tried it but it doesnt work. Please check it(http://stackoverflow.com/questions/3700650/datagridview-doesnt-work-in-vb-netit-shows-nothing) where I have written detail about my problem.
James
It shows "Cannot open database SUIMT requested by the login. The login failed. Login failed for user 'MY-PC\James'".
James
Does the user MY-PC\James exist on the database server?
Tony Abrams
I have used windows authentication so I dont have extra password for SQL SERVER 2000 personal edition. When I click on SQL Service Manager on my task bar it shows SERVER - MY-PC. So MY-PC is the exect name of server.
James
I dont have problem with ADODB even I have done same work in mshflexgrid but when I tried to build my program it shows error(because vb.net doesnt support mshflexgrid) as a result I am trying to use DataGridView. I am tired because it shows nothing!
James