在给出错误的代码中插入命令

时间:2016-03-16 21:00:17

标签: vb.net sql-insert detailsview

这可能是一个非常愚蠢的错误,但是gridview的状态以及何时发生/更改总是让我感到困惑。
我有一个详细信息视图作为输入。页脚中有一个“新建”按钮,用于将dv置于insertmode中。我想执行输入检查,因此aspx页面中的asp:sqldatasource是最小的,并且检查是在后面的代码中完成的。该方法可以采用的一个路径构造insertcommand,根据需要添加参数,并添加和输入数据库,但另一个路径只是向标签提供错误消息并结束方法。当这条路径发生时说“数据源[datasource]不支持插入,除非指定了insertcommand”,我在IE中得到一个调试器弹出窗口
DetailsView控件:

    <asp:DetailsView ID="dvInsertInterrupt" runat="server" AutoGenerateRows="false" DataSourceID="sqldsInsertInterrupt" CssClass="table table-striped table-bordered">
    <Fields>
    <asp:TemplateField HeaderText="Customer" InsertVisible="true" HeaderStyle-CssClass="col-xs-3 field-label active">
    <InsertItemTemplate>
    <asp:DropDownList ID="ddlNames" runat="server" DataSourceID="sqldsNames" DataTextField="Name" DataValueField="AccountNumber" CssClass="col-md-9 form-control" />
    </InsertItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Start Date" InsertVisible="true" HeaderStyle-CssClass="col-xs-3 field-label active">
    <InsertItemTemplate>
    <asp:Calendar ID="calStartDate" runat="server" CssClas="col-md-9 form-control" />
    </InsertItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Start Time" InsertVisible="true" HeaderStyle-CssClass="col-xs-3 field-label active">
    <InsertItemTemplate>
    <asp:TextBox ID="txtStartTime" runat="server" MaxLength="4" /><asp:Label ID="lblInfo" runat="server"><small> 24-Hour Format</small></asp:Label>
    </InsertItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Duration" InsertVisible="true" HeaderStyle-CssClass="col-xs-3 field-label active">
    <InsertItemTemplate>
    <asp:DropDownList ID="ddlHours" runat="server" CssClass="col-md-9 form-control">
    <asp:ListItem Text="2" Value="2" />
    <asp:ListItem Text="3" Value="3" />
    <asp:ListItem Text="4" Value="4" />
    <asp:ListItem Text="5" Value="5" />
    <asp:ListItem Text="6" Value="6" />
    <asp:ListItem Text="7" Value="7" />
    <asp:ListItem Text="8" Value="8" Selected="True" />
    </asp:DropDownList>
    </InsertItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ShowHeader="false" HeaderStyle-CssClass="field-label col-xs-3 active">
    <ItemTemplate>
    <asp:LinkButton ID="lbNew" runat="server" CausesValidation="false" CommandName="New" Text="New Interrupt" CssClass="btn btn-primary" />
    </ItemTemplate>
    <InsertItemTemplate>
    <asp:LinkButton ID="lbInsert" runat="server" CausesValidation="false" CommandName="Insert" Text="Insert" CssClass="btn btn-primary" />
    <asp:LinkButton ID="lbCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel" CssClass="btn btn-default" />
</InsertItemTemplate>
    </asp:TemplateField>
    </Fields>
    </asp:DetailsView>


数据源:

<asp:SqlDataSource ID="sqldsInsertInterrupt" runat="server" ConnectionString="<%$ ConnectionStrings:WebTestConnectionString %>"
        SelectCommand="SELECT * FROM Interrupt" InsertCommand="" />


方法背后的代码:

Protected Sub dvInsertInterrupt_RowCommand(ByVal sender As Object, ByVal e As DetailsViewCommandEventArgs) Handles dvInsertInterrupt.ItemCommand
        Try
            If e.CommandName = "Insert" Then
                Dim accountNumber As Integer = DirectCast(dvInsertInterrupt.Rows(0).Cells(1).Controls(1), DropDownList).SelectedValue
                Dim startDate As DateTime = DirectCast(dvInsertInterrupt.Rows(1).Cells(1).Controls(1), Calendar).SelectedDate
                Dim milTime As String = DirectCast(dvInsertInterrupt.Rows(2).Cells(1).Controls(1), TextBox).Text
                Dim hr As Integer = 0
                Dim min As Integer = 0
                Dim start As DateTime
                Dim hours As Integer = DirectCast(dvInsertInterrupt.Rows(3).Cells(1).Controls(1), DropDownList).SelectedValue
                Dim endDate As DateTime
                Dim message As String = ""

                If IsNumeric(milTime) Then
                    If milTime.Length < 4 Then
                        Select Case milTime.Length
                            Case Is < 2
                                milTime = "000" & milTime.ToString
                            Case Is < 3
                                milTime = "00" & milTime.ToString
                            Case Else
                                milTime = "0" & milTime.ToString
                        End Select
                        hr = Left(milTime, 2)
                        min = Right(milTime, 2)
                    Else
                        hr = Left(milTime, 2)
                        min = Right(milTime, 2)
                    End If

                    If hr < 0 Or hr > 23 Then
                        message = "Hour must be between 0 and 23"
                        DirectCast(dvInsertInterrupt.Rows(2).Cells(1).Controls(1), TextBox).Text = ""
                    Else
                        If min < 0 Or min > 59 Then
                            message = "Minutes must be between 0 and 59"
                            DirectCast(dvInsertInterrupt.Rows(2).Cells(1).Controls(1), TextBox).Text = ""
                        Else
                            start = New DateTime(startDate.Year, startDate.Month, startDate.Day, hr, min, 0)
                            endDate = start.AddHours(hours)
                        End If
                    End If
                Else
                    message = "Enter numeric values only"
                    DirectCast(dvInsertInterrupt.Rows(2).Cells(1).Controls(1), TextBox).Text = ""
                End If

                If Not CheckForInterrupt(accountNumber, start, endDate) Then
                    sqldsInsertInterrupt.InsertCommand = "INSERT INTO Interrupt (CustomerAccountNumber, StartDate, Hours, EndDate, Approved, SecondNotificationSent) VALUES (@accountNumber, @startDate, @hours, @endDate, 0, 0)"
                    sqldsInsertInterrupt.InsertParameters.Add("accountNumber", accountNumber)
                    sqldsInsertInterrupt.InsertParameters.Add("startDate", start)
                    sqldsInsertInterrupt.InsertParameters.Add("hours", hours)
                    sqldsInsertInterrupt.InsertParameters.Add("endDate", endDate)
                Else
                    lblInsertMessage.Text = "There is already an interrupt scheduled at that time!"

                End If

            End If
        Catch ex As Exception
            lblInsertMessage.Text = "**Error** - " & ex.ToString & " **"
        Finally
        End Try
    End Sub

0 个答案:

没有答案