I'm attempting to implement a javascript popup calendar in a DataGrid.
The calendar will be used to insert a date into a textbox. I keep
getting a "System.NullRef erenceException : Object reference not set to
an instance of an object" error, which points to the following line:
lnkCalendar.Att ributes("OnClic k") =
String.Format(" window.open('Ca lendar.aspx?tex tbox={0}','cal' ,'...')",
txtEstimatedDep loy)
Here is the complete code for the aspx page and its codebehind page:
aspx page:
<%@ Page Language="vb"
Src="BURSInsert Successful.aspx .vb"
Inherits="PSDat aGrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<%@ import Namespace="Syst em.Data" %>
<%@ import Namespace="Syst em.Data.SqlClie nt" %>
<%@ import Namespace="Syst em.Web.UI.WebCo ntrols" %>
<html>
<head>
<title>BURS - Insert Successful</title>
<style>
H3 {
font: 8pt arial;
font-weight: 600;
text-align: center;
}
H1 {
font: 8pt arial;
font-weight: 400;
text-align: center;
}
A {
font: 8pt arial;
font-weight: 400;
}
</style>
</head>
<body>
<h3>Corporate Information Services<br>Ins ert
Successful</h3>
<h1>A new project record has been created.<br>Cli ck
"<b><u>Edit </u></b>" in the last
column to make changes to the report.</h1>
<table>
<form id="Form1" method="post"
runat="server">
<asp:datagrid id="dgProjectSu mmary"
runat="server"
ShowHeader="Tru e"
ShowFooter="Fal se"
BorderColor="bl ack"
AutoGenerateCol umns="False"
OnEditCommand=" EditRecord"
OnUpdateCommand ="UpdateReco rd"
OnCancelCommand ="CancelEdit ">
<headerstyle BackColor="ligh tgray"
Font-Name="arial"
Font-Size="8"
Font-Bold="true"
ForeColor="blac k"
HorizontalAlign ="center"
/>
<itemstyle
Font-Name="arial"
Font-Size="8"
HorizontalAlign ="left"
VerticalAlign=" top"
/>
<columns>
<asp:templateco lumn>
<itemtemplate >
<b>Project ID:</b>
<asp:label text='<%#
Container.DataI tem("ProjectID" )%>'
font-name="arial"
font-size="8" runat="server"
ID="Label1" NAME="Label1"/>
</itemtemplate>
<edititemtempla te>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Project</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("ProjectNam e")%>'
font-name="arial"
font-size="8" width="70"
runat="server" ID="Label2"
NAME="Label2"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtProjectN ame"
text='<%#
Container.DataI tem("ProjectNam e")%>'
font-name="arial"
font-size="8" rows="10"
width="70" textmode="multi line"
wrap="true"
style="overflow :hidden"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Project Description</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("ProjectDes cription")%>'
font-name="arial"
font-size="8" width="100"
runat="server" ID="Label3"
NAME="Label3"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtProjectD escription"
text='<%#
Container.DataI tem("ProjectDes cription")%>'
font-name="arial" font-size="8"
rows="10" width="100"
textmode="multi line" wrap="true"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Phase</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("Phase")%>'
font-name="arial"
font-size="8" width="39"
runat="server" ID="Label4"
NAME="Label4"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtPhase" text='<%#
Container.DataI tem("Phase")%>'
font-name="arial"
font-size="8" rows="10"
width="39" textmode="multi line"
wrap="true"
style="overflow :hidden"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Estimated Deploy</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("EstimatedD eploy").ToShort DateString
%>' font-name="arial"
font-size="8" width="65"
runat="server" ID="Label5"
NAME="Label5"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtEstimate dDeploy"
text='<%#
Container.DataI tem("EstimatedD eploy").ToShort DateString
%>'
font-name="arial" font-size="8"
rows="1" width="65" runat="server">
</asp:textbox>
<asp:hyperlin k id="lnkCalendar "
runat="server" navigateurl="Ca lendar.aspx"
target="_blank" >Calendar</asp:hyperlink>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Recent Accomplishments </b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("RecentAcco mplishments")%> '
font-name="arial"
font-size="8" width="100"
runat="server" ID="Label6"
NAME="Label6"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox
id="txtRecentAc complishments" text='<%#
Container.DataI tem("RecentAcco mplishments")%> '
font-name="arial" font-size="8"
rows="10" width="100"
textmode="multi line" wrap="true"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Focus Items</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("FocusItems ")%>'
font-name="arial"
font-size="8" width="100"
runat="server" ID="Label7"
NAME="Label7"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtFocusIte ms"
text='<%# Container.DataI tem("FocusItems ")%>'
font-name="arial"
font-size="8" rows="10"
width="100" textmode="multi line"
wrap="true" runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Est. Benefits</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("EstBenefit s")%>'
font-name="arial"
font-size="8" width="51"
runat="server" ID="Label8"
NAME="Label8"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtEstBenef its"
text='<%#
Container.DataI tem("EstBenefit s")%>'
font-name="arial"
font-size="8" rows="10"
width="51" textmode="multi line"
wrap="true" style="overflow :hidden"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>IS Cost</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("ISCost")%> '
font-name="arial"
font-size="8" width="51"
runat="server" ID="Label9"
NAME="Label9"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtISCost" text='<%#
Container.DataI tem("ISCost")%> '
font-name="arial"
font-size="8" rows="10"
width="51" textmode="multi line"
wrap="true" style="overflow :hidden"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Corporate<br >IS Lead</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("CorporateI SLead")%>'
font-name="arial"
font-size="8" width="70"
runat="server" ID="Label10"
NAME="Label10"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtCorporat eISLead"
text='<%#
Container.DataI tem("CorporateI SLead")%>'
font-name="arial" font-size="8"
rows="10" width="70"
textmode="multi line" wrap="true"
style="overflow :hidden"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Business Lead</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("BusinessLe ad")%>'
font-name="arial"
font-size="8" width="70"
runat="server" ID="Label11"
NAME="Label11"/>
</itemtemplate>
<edititemtempla te>
<asp:textbox id="txtBusiness Lead"
text='<%#
Container.DataI tem("BusinessLe ad")%>'
font-name="arial" font-size="8"
rows="10" width="70"
textmode="multi line" wrap="true"
style="overflow :hidden"
runat="server">
</asp:textbox>
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Department </b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("Department ")%>'
font-name="arial"
font-size="8" width="100"
runat="server" ID="Label12"
NAME="Label12"/>
</itemtemplate>
<edititemtempla te>
<asp:listbox id="lbxDepartme nt"
font-name="arial" font-size="8"
width="100" rows="1"
DataTextField=" DepartmentName"
DataValueField= "DepartmentName " DataSource="<%#
GetDepartmentNa mes() %>"
runat="server" />
</edititemtemplat e>
</asp:templatecol umn>
<asp:templateco lumn>
<headertemplate >
<b>Status</b>
</headertemplate>
<itemtemplate >
<asp:label text='<%#
Container.DataI tem("Status")%> '
font-name="arial"
font-size="8" width="58"
runat="server" ID="Label13"
NAME="Label13"/>
</itemtemplate>
<edititemtempla te>
<asp:listbox id="lbxStatus"
font-name="arial" font-size="8"
width="58" rows="1"
DataTextField=" Status"
DataValueField= "Status" DataSource="<%#
GetStatus() %>"
runat="server" />
</edititemtemplat e>
</asp:templatecol umn>
<asp:editcomman dcolumn
ButtonType="Lin kButton" UpdateText="Upd ate"
CancelText="Can cel"
EditText="Edit" />
</columns>
</asp:datagrid>
<br>
<tr>
<td>
<asp:button id="btnReturn"
font-size="8" runat="server" text="Return to
Main Page" OnClick="btnRet urn_Click"/>
</td>
</tr>
<tr>
<td>
<asp:Button id="btnInsert"
font-size="8" runat="server" text="Insert
Another Project" OnClick="btnIns ert_Click"/>
</td>
</tr>
</form>
</table>
</body>
</html>
Codebehind page:
[code:1:0fdbcec5 83]
Imports System
Imports System.Collecti ons
Imports System.Data
Imports System.Data.Sql Client
Imports System.Web
Imports System.Web.UI.W ebControls
Public Class PSDataGrid
Inherits System.Web.UI.P age
Protected WithEvents dgProjectSummar y As
System.Web.UI.W ebControls.Data Grid
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Private Sub LoadGrid()
Dim intProjectID As Integer
intProjectID = Session("Projec tID")
Dim dsProjectSummar y As New DataSet()
Dim strConnection As String = "" 'Connection string
omitted from post
Dim objConnection As New SqlConnection(s trConnection)
Dim objCommand As New
SqlCommand("dbo .ap_DisplayNewP roject",
objConnection)
objCommand.Comm andType = CommandType.Sto redProcedure
Dim objParameter As New SqlParameter("@ ProjectID",
SqlDbType.Int)
objCommand.Para meters.Add(objP arameter)
objParameter.Di rection = ParameterDirect ion.Input
objParameter.Va lue = intProjectID
objConnection.O pen()
dgProjectSummar y.DataSource = objCommand.Exec uteReader()
dgProjectSummar y.DataBind()
objConnection.C lose()
End Sub
Public Sub btnReturn_Click (ByVal Source As Object, ByVal E As
EventArgs)
Response.Redire ct("default.htm ")
End Sub
Public Sub btnInsert_Click (ByVal Source As Object, ByVal E As
EventArgs)
Response.Redire ct("BURSProject Insert.aspx")
End Sub
Public Sub EditRecord(ByVa l Sender As Object, ByVal E As
DataGridCommand EventArgs)
dgProjectSummar y.EditItemIndex = E.Item.ItemInde x
LoadGrid()
End Sub
Public Sub CancelEdit(ByVa l Sender As Object, ByVal E As
DataGridCommand EventArgs)
dgProjectSummar y.EditItemIndex = -1
LoadGrid()
End Sub
Public Sub UpdateRecord(By Val Sender As Object, ByVal E As
DataGridCommand EventArgs)
Dim strConnection As String = "" 'Connection string
omitted from post
Dim objConnection As New SqlConnection(s trConnection)
Dim objCommand As New
SqlCommand("ap_ UpdateProjectSu mmary",
objConnection)
Dim idProjectName As String =
CType(e.Item.Fi ndControl("txtP rojectName"),
TextBox).Text
Dim idProjectDescri ption As String =
CType(e.Item.Fi ndControl("txtP rojectDescripti on"),
TextBox).Text
Dim idPhase As String =
CType(e.Item.Fi ndControl("txtP hase"),
TextBox).Text
Dim idEstimatedDepl oy As String =
CType(e.Item.Fi ndControl("txtE stimatedDeploy" ),
TextBox).Text
Dim idRecentAccompl ishments As String =
CType(e.Item.Fi ndControl("txtR ecentAccomplish ments"),
TextBox).Text
Dim idFocusItems As String =
CType(e.Item.Fi ndControl("txtF ocusItems"),
TextBox).Text
Dim idEstBenefits As String =
CType(e.Item.Fi ndControl("txtE stBenefits"),
TextBox).Text
Dim idISCost As String =
CType(e.Item.Fi ndControl("txtI SCost"),
TextBox).Text
Dim idCorporateISLe ad As String =
CType(e.Item.Fi ndControl("txtC orporateISLead" ),
TextBox).Text
Dim idBusinessLead As String =
CType(e.Item.Fi ndControl("txtB usinessLead"),
TextBox).Text
objCommand.Comm andType = CommandType.Sto redProcedure
Dim objParameter1 As New
SqlParameter("@ ProjectName", SqlDbType.VarCh ar,
100)
objCommand.Para meters.Add(objP arameter1)
objParameter1.D irection = ParameterDirect ion.Input
objParameter1.V alue = idProjectName
Dim objParameter2 As New
SqlParameter("@ ProjectDescript ion", SqlDbType.VarCh ar,
2000)
objCommand.Para meters.Add(objP arameter2)
objParameter2.D irection = ParameterDirect ion.Input
objParameter2.V alue = idProjectDescri ption
Dim objParameter3 As New SqlParameter("@ Phase",
SqlDbType.VarCh ar, 30)
objCommand.Para meters.Add(objP arameter3)
objParameter3.D irection = ParameterDirect ion.Input
objParameter3.V alue = idPhase
Dim objParameter4 As New
SqlParameter("@ EstimatedDeploy ",
SqlDbType.DateT ime)
objCommand.Para meters.Add(objP arameter4)
objParameter4.D irection = ParameterDirect ion.Input
objParameter4.V alue = idEstimatedDepl oy
Dim objParameter5 As New
SqlParameter("@ RecentAccomplis hments",
SqlDbType.VarCh ar, 2000)
objCommand.Para meters.Add(objP arameter5)
objParameter5.D irection = ParameterDirect ion.Input
objParameter5.V alue = idRecentAccompl ishments
Dim objParameter6 As New SqlParameter("@ FocusItems",
SqlDbType.VarCh ar, 2000)
objCommand.Para meters.Add(objP arameter6)
objParameter6.D irection = ParameterDirect ion.Input
objParameter6.V alue = idFocusItems
Dim objParameter7 As New
SqlParameter("@ EstBenefits", SqlDbType.VarCh ar,
200)
objCommand.Para meters.Add(objP arameter7)
objParameter7.D irection = ParameterDirect ion.Input
objParameter7.V alue = idEstBenefits
Dim objParameter8 As New SqlParameter("@ ISCost",
SqlDbType.VarCh ar, 15)
objCommand.Para meters.Add(objP arameter8)
objParameter8.D irection = ParameterDirect ion.Input
objParameter8.V alue = idISCost
Dim objParameter9 As New
SqlParameter("@ CorporateISLead ", SqlDbType.VarCh ar,
50)
objCommand.Para meters.Add(objP arameter9)
objParameter9.D irection = ParameterDirect ion.Input
objParameter9.V alue = idCorporateISLe ad
Dim objParameter10 As New
SqlParameter("@ BusinessLead", SqlDbType.VarCh ar,
50)
objCommand.Para meters.Add(objP arameter10)
objParameter10. Direction = ParameterDirect ion.Input
objParameter10. Value = idBusinessLead
objConnection.O pen()
dgProjectSummar y.DataSource = objCommand.Exec uteReader()
dgProjectSummar y.DataBind()
objConnection.C lose()
dgProjectSummar y.EditItemIndex = -1
LoadGrid()
End Sub
Public Sub Item_Bound(Send er As Object, E As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
dgProjectSummar y.ItemDataBound
Dim txtEstimatedDep loy As TextBox
txtEstimatedDep loy =
e.Item.FindCont rol("txtEstimat edDeploy")
Dim lnkCalendar As Hyperlink
lnkCalendar = e.Item.FindCont rol("lnkCalenda r")
lnkCalendar.Att ributes("OnClic k") =
String.Format(" window.open('Ca lendar.aspx?tex tbox={0}','cal' ,'...')",
txtEstimatedDep loy)
End Sub
Public Function GetDepartmentNa mes() As ICollection
Dim strConnection As String = "" 'Connection string
omitted from post
Dim objConnection As New SqlConnection(s trConnection)
Dim daDeptNames As SqlDataAdapter = New
SqlDataAdapter( "SELECT DepartmentName FROM Department",
objConnection)
Dim dsDeptNames As DataSet = New DataSet()
daDeptNames.Fil l(dsDeptNames)
Return dsDeptNames.Tab les(0).DefaultV iew
End Function
Public Function GetStatus() As ICollection
Dim strConnection As String = "" 'Connection string
omitted from post
Dim objConnection As New SqlConnection(s trConnection)
Dim daStatus As SqlDataAdapter = New
SqlDataAdapter( "SELECT Status FROM Status",
objConnection)
Dim dsStatus As DataSet = New DataSet()
daStatus.Fill(d sStatus)
Return dsStatus.Tables (0).DefaultView
End Function
End Class
[/code:1:0fdbcec5 83]
I'm rather new to programming in .NET so any suggestions will be
appreciated.
*-----------------------*
Posted at:
www.GroupSrv.co m
*-----------------------*