help with error message using dataset
This is a .ascx page that I've coded using ASP.net but I keep getting the following error "System.NullReferenceException: Object reference not set to an instance of an object."
the error line mentioned is
Code:
cmdSelect.SelectCommand = cmdSql
Code:
<%@ Control Language="VB" AutoEventWireup="true" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
Public FirstOfMonth As DateTime
Dim Today As DateTime
Dim Connection As SqlConnection
Dim Sql As String
Dim cmdSql As SqlCommand
Dim dts As DataSet
Dim cmdSelect As SqlDataAdapter
Sub Page_Load(s As Object, e As EventArgs)
Connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Connection.Open()
Today = DateTime.Today
Call ShowMonth
End Sub
Public Sub ShowMonth()
Dim currentDate As DateTime = firstOfMonth
Dim endDate As DateTime = firstOfMonth.AddMonths(1).AddSeconds(-1)
Dim startDOW As DayOfWeek = firstOfMonth.DayOfWeek
Dim endDOW As DayOfWeek = endDate.DayOfWeek
Dim months As String() = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
monthTitle.Text = months(firstOfMonth.Month.tostring) + " " + firstOfMonth.Year.tostring
Dim daysInWeek As Integer = 0
Dim daysInMonth As Integer = 0
Dim startDayOfWeek As Integer = 0
Dim endDayOfWeek As Integer = 0
Select startDOW
Case DayOfWeek.Sunday: startDayOfWeek = 0
Case DayOfWeek.Monday: startDayOfWeek = 1
Case DayOfWeek.Tuesday: startDayOfWeek = 2
Case DayOfWeek.Wednesday: startDayOfWeek = 3
Case DayOfWeek.Thursday: startDayOfWeek = 4
Case DayOfWeek.Friday: startDayOfWeek = 5
Case DayOfWeek.Saturday: startDayOfWeek = 6
End Select
Select endDOW
Case DayOfWeek.Sunday: endDayOfWeek = 0
Case DayOfWeek.Monday: endDayOfWeek = 1
Case DayOfWeek.Tuesday: endDayOfWeek = 2
Case DayOfWeek.Wednesday: endDayOfWeek = 3
Case DayOfWeek.Thursday: endDayOfWeek = 4
Case DayOfWeek.Friday: endDayOfWeek = 5
Case DayOfWeek.Saturday: endDayOfWeek = 6
End Select
Dim cal As StringBuilder = New StringBuilder
cal.Append("<tr>")
Dim i As Integer = 0
While i < startDayOfWeek
cal.Append("<td height='100' class='calendarNonDay' bgcolor='#999999' width='82'> </td>")
System.Math.Min(System.Threading.Interlocked.Increment(daysInWeek),daysInWeek-1)
System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)
End While
'dim db As Database = DatabaseFactory.CreateDatabase
'Dim dbCom As DbCommand
While daysInMonth < endDate.Day
If daysInWeek = 0 Then
cal.Append("<tr>")
End If
System.Math.Min(System.Threading.Interlocked.Increment(daysInMonth),daysInMonth-1)
Dim thisDate As String = firstOfMonth.Month.tostring + "/" + daysInMonth.tostring + "/" + firstOfMonth.Year.tostring
Sql = "SELECT chapter_name FROM event WHERE CAST(MONTH(event_date) AS VARCHAR(2)) + '/' + CAST(DAY(event_date) AS VARCHAR(2)) + '/' + CAST(YEAR(event_date) AS VARCHAR(4)) = '" + thisDate + "' AND active = 'Y' ORDER BY chapter_id ASC"
cmdSql = New SqlCommand(sql, Connection)
cmdSelect.SelectCommand = cmdSql
cmdSelect.Fill(dts, "event_id")
'dts = db.ExecuteDataSet(cmdSql)
'dts.Close()
'SqldbCom = db.GetSqlStringCommand("SELECT organization FROM pfa_event WHERE CAST(MONTH(event_date) AS VARCHAR(2)) + '/' + CAST(DAY(event_date) AS VARCHAR(2)) + '/' + CAST(YEAR(event_date) AS VARCHAR(4)) = '" + thisDate + "' AND active = 'Y' ORDER BY organization ASC")
'Dim ds As DataSet = db.ExecuteDataSet(dbCom)
Dim events As StringBuilder = New StringBuilder
Dim j As Integer = 0
While j < dts.Tables(0).Rows.Count
events.Append("<div align='center' width='82'><A HREF='ViewDate.aspx?date=" + thisDate + "' class='normallink'>" + dts.Tables(0).Rows(j)("organization").ToString + " Event</A></div><BR>")
System.Math.Min(System.Threading.Interlocked.Increment(j),j-1)
End While
Dim color As String = "#EEEEEE"
If firstOfMonth.Month = today.Month AndAlso daysInMonth = today.Day AndAlso firstOfMonth.Year = today.Year Then
color = "#FFCCCC"
Else
If events.Length > 0 Then
color = "#FFFFCC"
End If
End If
cal.Append("<td height='100' class='calendarDate' bgcolor='" + color + "' valign='top' align='left' width='82'>" + daysInMonth + "<BR>")
cal.Append(events)
cal.Append("</td>")
System.Math.Min(System.Threading.Interlocked.Increment(daysInWeek),daysInWeek-1)
If daysInWeek = 7 Then
cal.Append("</tr>")
daysInWeek = 0
End If
End While
'Dim i As Integer = daysInWeek
i = daysInWeek
While i < 7
cal.Append("<td height='100' class='calendarNonDay' bgcolor='#999999'> </td>")
System.Math.Min(System.Threading.Interlocked.Increment(daysInWeek),daysInWeek-1)
System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)
End While
cal.Append("</tr>")
calendarWeeks.Text = cal.ToString
End Sub
</script>
<table width="550" border="0" cellpadding="1" cellspacing="1" bgcolor="black">
<!-- Calendar Title Row -->
<tr>
<td colspan="7" bgcolor="#334B79" align="center"> <asp:Label ID="monthTitle" runat="server" Font-Bold="True" Font-Size="15pt" ForeColor="White"></asp:Label></td>
</tr>
<tr>
<td bgcolor="#EC9900" align="center" width="82"><b>Sunday</b></td>
<td bgcolor="#EC9900" align="center" width="82"><b>Monday</b></td>
<td bgcolor="#EC9900" align="center" width="82"><b>Tuesday</b></td>
<td bgcolor="#EC9900" align="center" width="82"><b>Wednesday</b></td>
<td bgcolor="#EC9900" align="center" width="82"><b>Thursday</b></td>
<td bgcolor="#EC9900" align="center" width="82"><b>Friday</b></td>
<td bgcolor="#EC9900" align="center" width="82"><b>Saturday</b></td>
</tr>
<asp:Label ID="calendarWeeks" runat="server" />
</table>
Re: help with error message using dataset
You forgot to instantiate the cmdSelect SqlDataAdapter.
VB Code:
cmdSql = New SqlCommand(Sql, Connection)
[b]cmdSelect = New SqlDataAdapter[/b] 'add this
cmdSelect.SelectCommand = cmdSql
'Also
dts = New DataSet()
cmdSelect.Fill(dts, "event_id")
Re: help with error message using dataset
I made the changes you suggested but now I get the error with
Code:
cmdSelect.Fill(dts, "event_id")
It says SelectCommand.Connection property has not been initialized