Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
C#
ExecuteReader: CommandText property has not been initialized


What I have tried:

VB
Dim cnn1 As SqlConnection
        Dim cmd1 As SqlCommand
        Dim reader1 As SqlDataReader

        connetionString = ConfigurationManager.ConnectionStrings("APMCUBIntranetConnectionString").ConnectionString
        cnn1 = New SqlConnection(connetionString)
        cnn1.Open()
        cmd1 = New SqlCommand(Sql1, cnn1)

        Sql1 = "SELECT Count([Questions]) As Tq, SUM(case when [Compliance]= 'Yes       ' then 1 else 0 end) As Yes, sum(case when [Compliance]= 'No        ' then 1 else 0 end) As No,Sum(case when [Compliance]= 'NA        ' then 1 else 0 end) As NA, (Count  ([Questions]) - Sum(case when [Compliance]= 'NA        ' then 1 else 0 end)) As Net FROM [APMCUBIntranet].[dbo].[AuditChaptHo]  where Branch = '" & DropDownList6.Text & "' and Chapter ='CREDIT MANAGEMENT' and SubChap1 ='A.Common Observations'  and  convert(datetime,AuditStDt,103) between convert(datetime, '" & strDate1 & "',103) and convert(datetime,'" & strDate2 & "',103) "
        
        reader1 = cmd1.ExecuteReader()
        While (reader1.Read())

            LblTQ1.Text = reader1(0)
            LblNA1.Text = reader1(3)
            LblNet1.Text = reader1(4)
            LblYes1.Text = reader1(1)
            LblMks1.Text = reader1(1)


        End While
        cnn1.Close()
        reader1.Close()
Posted
Updated 28-Sep-16 21:30pm
v2

1 solution

Your command object should be initialized after you define the query like -

VB
'cmd1 = New SqlCommand(Sql1, cnn1)
Sql1 = "SELECT Count([Questions]) As Tq, SUM(case when [Compliance]= 'Yes ' then 1 else 0 end) As Yes, sum(case when [Compliance]= 'No ' then 1 else 0 end) As No,Sum(case when [Compliance]= 'NA ' then 1 else 0 end) As NA, (Count ([Questions]) - Sum(case when [Compliance]= 'NA ' then 1 else 0 end)) As Net FROM [APMCUBIntranet].[dbo].[AuditChaptHo] where Branch = '" & DropDownList6.Text & "' and Chapter ='CREDIT MANAGEMENT' and SubChap1 ='A.Common Observations' and convert(datetime,AuditStDt,103) between convert(datetime, '" & strDate1 & "',103) and convert(datetime,'" & strDate2 & "',103) "
cmd1 = New SqlCommand(Sql1, cnn1)


Hope, it helps :)
 
Share this answer
 
v4
Comments
Nagaraju Kukkudala 29-Sep-16 3:35am    
Yea..It works... :) Tq
Suvendu Shekhar Giri 29-Sep-16 3:37am    
Glad that it helped :)

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

  Print Answers RSS


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