从查找表中更新动态下拉列表中的表

时间:2014-09-18 14:41:32

标签: asp.net sql-server vb.net gridview

我不知道这是否是由于数据库设计不佳造成的,但是自昨晚以来我一直在努力通过从查找中动态填充的下拉列表中选择要更新的记录值来更新某些记录表

基本上,我们有三个名为Courses,Instructors和CourseInstructor表的表。

CourseInstructor表是Courses和Instructor表之间的桥接表。

因此,在向各种表中添加课程和教师时,他们的外键会自动添加到桥接表中。

到目前为止,这似乎工作正常。

但是,用户在更改这些表时遇到了困难。

例如,如果用户希望将一名教师替换为另一名教师,那么她至今没有这样做。更新后,该消息表明成功更新了教师姓名不会更改。

任何想法我可能做错了下面的代码?

        <asp:TemplateField HeaderText="Instructor">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlInstructors" runat="server" AppendDataBoundItems="True" DataSourceID="SubjectDataSource"
                    DataTextField="InstructorName" DataValueField="InstructorId">
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblInstructors" runat="server" Text='<% #Bind("InstructorName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

          <asp:SqlDataSource ID="sqlDataSourceloc" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DBConn %>" 
    SelectCommand="SELECT locationId, Location FROM Locations order by location asc"></asp:SqlDataSource>
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
     UpdateCommand = "Update tblCourseInstructor Set CourseId = @CourseId where (CourseId =@CourseId) Update tblCourseInstructor set InstructorId=@InstructorId where (instructorId=@instructorId and courseId = @courseId)"
     SelectCommand="select l.locationid,c.CourseId, i.instructorId,CourseName, i.instructorname, dbo.fnFormatDate(t.trainingDates, 'MON/DD/YYYY') trainingDates, t.trainingTime,CourseDescription from Courses c, tblLocations l, TrainingDates t, Instructors i, CourseInstructor ci where l.locationid = c.locationid and c.dateId = t.dateId and i.instructorid =  ci.instructorId and c.courseid=ci.courseid and YEAR(t.trainingDates) = YEAR(getDate())"
                   FilterExpression="LocationId = '{0}'" >
<FilterParameters>
 <asp:ControlParameter ControlID="ddlLocation" Name="LocationId" PropertyName="SelectedValue" Type="Int32" />
 </FilterParameters>              
    <UpdateParameters>
        <asp:Parameter Name="CourseId" Type="Int32" />
        <asp:Parameter Name="instructorId" Type="Int32" />
    </UpdateParameters>
   </asp:SqlDataSource>
   <asp:SqlDataSource ID="SubjectDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DBConn %>"
    SelectCommand="SELECT instructorId, InstructorName FROM dbo.Instructors order by InstructorName">
   </asp:SqlDataSource>   


Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim ddAssigned As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlInstructors"), DropDownList)
        e.NewValues("instructorId") = ddAssigned.SelectedValue
        SubjectDataSource.DataBind()

    End Sub

0 个答案:

没有答案