Mikerhonda
asked on
ASP.Net Datagrid with Dropdownlists
I have a datagrid that uses templates for data entry and for adding additional rows to the table. The problem I am having is that if the primary table row contains a null value, I get a "Cast from type 'DBNull' to type 'String' is not valid" error. I basically have 3 questions:
1. How can I modify this code to be able to handle nulls in the primary table?
2. How can I handle situations where the child table has only text values for the dropdownlist and no indexes?
3. How can I make the dropdownlist show empty until I choose a name when I'm adding a row? Currently it will show the first item in the list.
'Code behind
'Function that returns the list during add mode
Public Function FillDesignChampion()
ds = SqlHelper.ExecuteDataset(o Conn, CommandType.StoredProcedur e, "SP_DBUserList")
Return ds
End Function
'Function that returns the list and the text value of the index during normal edit mode.
Function GetDesignChampionIndex(ByV al CatID As String) As Integer
Dim iLoop As Integer
ds = SqlHelper.ExecuteDataset(o Conn, CommandType.StoredProcedur e, "SP_DBUserList")
tblOriginator = ds.Tables(0)
For iLoop = 0 To tblOriginator.Rows.Count - 1
If Int32.Parse(CatID) = _
Int32.Parse(tblOriginator. Rows(iLoop )("Origina torID")) Then
Return iLoop
End If
Next iLoop
End Function
'HTML Code
<asp:TemplateColumn HeaderText="Design Champion">
<ItemTemplate>
<asp:DropDownList id=editDesignChampion runat="server" Width="115px" SelectedIndex='<%# GetDesignChampionIndex(Con tainer.Dat aItem("Des ignChampio n")) %>' DataSource="<%# FillDesignChampion() %>" DataTextField="EmployeeNam e" DataValueField="Originator ID">
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList id=addDesignChampion runat="server" Width="115px" DataSource="<%# FillDesignChampion() %>" DataTextField="EmployeeNam e" DataValueField="Originator ID">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateColumn>
End Function
1. How can I modify this code to be able to handle nulls in the primary table?
2. How can I handle situations where the child table has only text values for the dropdownlist and no indexes?
3. How can I make the dropdownlist show empty until I choose a name when I'm adding a row? Currently it will show the first item in the list.
'Code behind
'Function that returns the list during add mode
Public Function FillDesignChampion()
ds = SqlHelper.ExecuteDataset(o
Return ds
End Function
'Function that returns the list and the text value of the index during normal edit mode.
Function GetDesignChampionIndex(ByV
Dim iLoop As Integer
ds = SqlHelper.ExecuteDataset(o
tblOriginator = ds.Tables(0)
For iLoop = 0 To tblOriginator.Rows.Count - 1
If Int32.Parse(CatID) = _
Int32.Parse(tblOriginator.
Return iLoop
End If
Next iLoop
End Function
'HTML Code
<asp:TemplateColumn HeaderText="Design Champion">
<ItemTemplate>
<asp:DropDownList id=editDesignChampion runat="server" Width="115px" SelectedIndex='<%# GetDesignChampionIndex(Con
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList id=addDesignChampion runat="server" Width="115px" DataSource="<%# FillDesignChampion() %>" DataTextField="EmployeeNam
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateColumn>
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For number 1 you could also try editing your stored procedure like:
ISNULL(COLUMN_NAME, '')
I haven't tried that myself yet, but I think it will work.
-------------------------- ---------- ---------- ---------- ---
The way that I handle number 2 myself is this:
I also add a label to the template column and set its text equal to the value you want selected, but set visible = false. Something like:
<asp:Label ID="lblEditParent" Runat="server" Width="0%" Visible="False" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName") %>'>
</asp:Label>
Then, at the end of your subroutine where you bind the dropdown list add the following:
Dim lbl As Label = e.Item.FindControl("lblEdi tDesignCha mpion")
Dim sSelected = CStr(lbl.Text)
If sSelected <> "" Then
DDL.Items.FindByText(sSele cted).Sele cted = True
End If
ISNULL(COLUMN_NAME, '')
I haven't tried that myself yet, but I think it will work.
--------------------------
The way that I handle number 2 myself is this:
I also add a label to the template column and set its text equal to the value you want selected, but set visible = false. Something like:
<asp:Label ID="lblEditParent" Runat="server" Width="0%" Visible="False" Text='<%# DataBinder.Eval(Container,
</asp:Label>
Then, at the end of your subroutine where you bind the dropdown list add the following:
Dim lbl As Label = e.Item.FindControl("lblEdi
Dim sSelected = CStr(lbl.Text)
If sSelected <> "" Then
DDL.Items.FindByText(sSele
End If
For number 1 you could also try something like:
<asp:DropDownList id=editDesignChampion runat="server" Width="115px" SelectedIndex='<%# GetDesignChampionIndex(Con tainer.Dat aItem("Des ignChampio n")) %>' DataSource="<%# FillDesignChampion() %>"
DataTextField='<%# iif(Databinder.Eval(Contai ner, "DataItem.EmployeeName") = NULL, "", Databinder.Eval(Container, "DataItem.EmployeeName"))% >' DataValueField="Originator ID">
</asp:DropDownList>
<asp:DropDownList id=editDesignChampion runat="server" Width="115px" SelectedIndex='<%# GetDesignChampionIndex(Con
DataTextField='<%# iif(Databinder.Eval(Contai
</asp:DropDownList>
ASKER
Thanks for the suggestions Torrwin. The final solution was a combination of lots of things. I decided to take care of everything is the VB code behind. All of this is handled in the ItemDataBound event of the datagrid. Here's the corrected code:
'Code behind
'Function that returns the DropDownList
FillDropDown("SP_Operation ", editOperationPFMEA, "OperationNumber", "OperationNumber", "@CPID", CPID)
Public Function FillDropDown(ByVal strProcedure As String, ByVal ddlDropdown As DropDownList, ByVal strTextField As String, ByVal strValueField As String, Optional ByVal strParameter As String = "", Optional ByVal strParameterValue As String = "")
Try
If strParameter = "" Then
ds = SqlHelper.ExecuteDataset(o Conn, CommandType.StoredProcedur e, strProcedure)
Else
ds = SqlHelper.ExecuteDataset(o Conn, CommandType.StoredProcedur e, strProcedure, New SqlClient.SqlParameter(str Parameter, strParameterValue))
End If
With ddlDropdown
.DataSource = ds
.DataTextField = strTextField
.DataValueField = strValueField
.DataBind()
.Items.Insert(0, "")
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
'Function that returns the text value of the DropDownList during normal edit mode.
GetSelectedValue("SP_Contr olPlanPFME ARowSelect ", editOperationPFMEA, "Operation#", "@PFMEAID", DataGridPFMEA.DataKeys(e.I tem.ItemIn dex))
Public Function GetSelectedValue(ByVal strProcedure As String, ByVal ddlDropdown As DropDownList, ByVal strTextField As String, Optional ByVal strParameter As String = "", Optional ByVal strParameterValue As String = "")
Try
dr = SqlHelper.ExecuteReader(oC onn, CommandType.StoredProcedur e, strProcedure, New SqlClient.SqlParameter(str Parameter, strParameterValue))
If dr.HasRows Then
dr.Read()
With ddlDropdown
.SelectedValue = CheckNull(dr(strTextField) )
End With
dr.Close()
End If
Catch ex As Exception
dr.Close()
MsgBox(ex.Message)
End Try
dr.Close()
End Function
Public Function CheckNull(ByVal Value)
If IsDBNull(Value) Then
Return ""
Else
Return Value
End If
End Function
'Code behind
'Function that returns the DropDownList
FillDropDown("SP_Operation
Public Function FillDropDown(ByVal strProcedure As String, ByVal ddlDropdown As DropDownList, ByVal strTextField As String, ByVal strValueField As String, Optional ByVal strParameter As String = "", Optional ByVal strParameterValue As String = "")
Try
If strParameter = "" Then
ds = SqlHelper.ExecuteDataset(o
Else
ds = SqlHelper.ExecuteDataset(o
End If
With ddlDropdown
.DataSource = ds
.DataTextField = strTextField
.DataValueField = strValueField
.DataBind()
.Items.Insert(0, "")
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
'Function that returns the text value of the DropDownList during normal edit mode.
GetSelectedValue("SP_Contr
Public Function GetSelectedValue(ByVal strProcedure As String, ByVal ddlDropdown As DropDownList, ByVal strTextField As String, Optional ByVal strParameter As String = "", Optional ByVal strParameterValue As String = "")
Try
dr = SqlHelper.ExecuteReader(oC
If dr.HasRows Then
dr.Read()
With ddlDropdown
.SelectedValue = CheckNull(dr(strTextField)
End With
dr.Close()
End If
Catch ex As Exception
dr.Close()
MsgBox(ex.Message)
End Try
dr.Close()
End Function
Public Function CheckNull(ByVal Value)
If IsDBNull(Value) Then
Return ""
Else
Return Value
End If
End Function
ASKER
Thanks for the quick reply. Lemme take these one at a time:
1. I need to retrieve all of the data in the parent table-even rows that have a null value for this field. I just need for the dropdownlist box to be able to handle it.
2. That was my thought too but if you look at the html code, it uses 'SelectedINDEX' and if I don't use an integer, I get a whole different error-sorry don't remember what the error says.
3. Am doing that now, and you're right, this should work.
-Mike