Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList null value

Status
Not open for further replies.

byurow

Programmer
Jul 7, 2002
111
0
0
US
Ok, I can't figure this one out. I have a grid with a combo box that has the following code:
Code:
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Day of the Week" SortExpression="Day_of_Week">
                <EditItemTemplate>
                    <asp:DropDownList ID="EditDay_of_Week" runat="server" 
                        DataSourceID="EditDay_of_WeekDataSource" DataTextField="Day_of_Week" 
                        DataValueField="Day_of_Week_ID" SelectedValue='<%# Bind("Day_of_Week_ID") %>'
                        AppendDataBoundItems="true">
                        <asp:ListItem Value=""></asp:ListItem>
                    </asp:DropDownList>
                    <asp:ObjectDataSource ID="EditDay_of_WeekDataSource" runat="server" 
                        OldValuesParameterFormatString="{0}" SelectMethod="GetDay_of_Week" 
                        TypeName="DayofWeekBLL"></asp:ObjectDataSource>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="NewDay_of_Week_ID" runat="server" DataSourceID="Day_of_WeekDataSource" 
                        DataTextField="Day_of_Week" DataValueField="Day_of_Week_ID" Height="16px" SkinID="BlueGV" 
                        Width="74px" SelectedValue='<%# Bind("Day_of_Week_ID") %>'
                        AppendDataBoundItems="true">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                   </asp:DropDownList>
                    <asp:ObjectDataSource ID="Day_of_WeekDataSource" runat="server" 
                        OldValuesParameterFormatString="{0}" SelectMethod="GetDay_of_Week" 
                        TypeName="DayofWeekBLL"></asp:ObjectDataSource>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label8" runat="server" Text='<%# Bind("Day_of_Week") %>'></asp:Label>
                </ItemTemplate>
                <FooterStyle VerticalAlign="Top" />
                <ItemStyle Width="75px" />
On update I recieve the following error:
Code:
{"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Tbl_Recurring_Event_Tbl_Day_of_Week". The conflict occurred in database "Brenda_Test", table "dbo.Tbl_Day_of_Week", column 'Day_of_Week_ID'. The statement has been terminated."}
It is trying to put a value of 0 into the table when the referenced table does not contain a record with an ID of 0. The following is the insert code:
Code:
   Protected Sub ObjectDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource1.Inserting

        Dim NewDay_of_Week_ID As DropDownList = gv.FooterRow.FindControl("NewDay_of_Week_ID")

        Dim Day_of_Week As Nullable(Of Short) = Nothing
        If Not String.IsNullOrEmpty(NewDay_of_Week_ID.SelectedValue) Then
            Day_of_Week = NewDay_of_Week_ID.SelectedValue
            e.InputParameters("Day_of_Week_ID") = Convert.ToInt32(NewDay_of_Week_ID.SelectedValue)
        End If

 End Sub
Code:
    <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Insert, True)> _
    Public Function AddRecurring_Event( _
        ByVal Recurring_Event_ID As Integer, ByVal Day_of_Week As String) _
        As Boolean

        Dim Recurring_Events As New CablesLog.Tbl_Recurring_EventDataTable()
        Dim Recurring_Event As CablesLog.Tbl_Recurring_EventRow = Recurring_Events.NewTbl_Recurring_EventRow

        Recurring_Event.Day_of_Week_ID = Day_of_Week_ID
        Recurring_Events.AddTbl_Recurring_EventRow(Recurring_Event)
        Dim rowsAffected As Integer = Adapter.Update(Recurring_Events)

        Return rowsAffected = 1
    End Function
The Day_of_Week_ID is not a required field on the Master table. HELP! Thanks!

Brenda
 
the problem is your using a datasource control. scrap the datasource and bind directly to the grid. then define the required grid events (sorting, paging, editing, updating, etc.) in code

benefits
1. you will understand what the code is doing, because you wrote it.
2. you can debug the problem
3. its the first step in impelmenting separation of concerns

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the reply Jason.

This is actually the first .Net app I have written (basically I'm a DBA with time on my hands, so I am playing).

I have built this app by using the tutorial from Microsoft. Could you point me in the direction of a sample app that shows how to bind directly to the grid?

Thanks again!

Brenda
 
delete the datasource control from your page.
then in code behind...
Code:
protected void Page_Load(object sender, EventArgs e)
{
   if(IsPostback) return;
   MyGridView.DataSource = new SomeService.GetData([with parameters]);
   MyGridView.DataBind();
}
where SomeSerivce is a class you define and GetData() returns IEnumerable at a minimum.

this is the most simple example. basically we first check to see if we are returning to the same page. if so don't do anything (because viewstate will take over). if not, we need to bind the data to the grid.

If you want sorting/paging/editing/deleting. you need to define how to handle those events. Either in markup, or in code behind(my preference) wire handlers to the associated events. see the MSDN docs for how to use the events.

While MS does make it easy to get up and running, they also make it very easy to adopt poor programming habits. if you have the time to research I would recommend reading about Domain Driven Design, S.O.L.I.D. design principles, unit testing, ORM as a data access tool, test driven development, behavior driven development, Design by Contract (program to the interface, not the implementation), and other popular buzzwords.

each can be examined in it own little world, but when you combine them together you get very rich, maintainable applications. that usually look nothing like MS code samples.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Great! Thanks so much. I will look into it.

Brenda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top