数据库不使用带有MS访问权限的网格视图进行更新asp.net(vb)

时间:2016-06-09 14:02:43

标签: sql asp.net

嘿伙计们,我的代码存在问题。 基本上,想要在网格视图中放入来自DB的行,这些行具有与DropDownlist上选择的状态相同的状态。此外,我希望能够修改它们。 select命令工作正常,但更新不起作用。 当我点击它时,它刷新页面(Autopostback)但在gridview上没有任何反应。 我真的尝试了一切,但这对我没有任何意义。 这是标记:

<form runat="server" autocomplete="off">
<br>
<asp:dropdownlist runat="server" id="Ddl1" OnSelectedIndexChanged="admvis"  Autopostback="True" >
    <asp:listitem>-</asp:listitem>
    <asp:listitem>In Configuration</asp:listitem>
    <asp:listitem>Configured</asp:listitem>
    <asp:listitem>Ordered</asp:listitem>
    <asp:listitem>Shipped</asp:listitem>
</asp:dropdownlist>

<asp:sqlDataSource id="SqlDataSource2"
               SelectCommand="SELECT Distinct State From PCRequests"
               runat="server"
               ConnectionString="<%$ connectionStrings:CyberTailorsDB%>"
               ProviderName="<%$ connectionStrings:CyberTailorsDB.providerName %>">
</asp:sqlDataSource> 


<asp:sqlDataSource id="SqlDataSource1"
               runat="server"
               SelectCommand=""
               UpdateCommand=""
               ConnectionString = "<%$ connectionStrings:CyberTailorsDB%>"
               ProviderName = "<%$ connectionStrings:CyberTailorsDB.providerName %>">

               <UpdateParameters>
                    <asp:Parameter Name="PCUse" Type="String"/>
                    <asp:Parameter Name="OS" Type="String"/>
                    <asp:Parameter Name="ExtDev" Type="String"/>
                    <asp:Parameter Name="Requests" Type="String"/>
                    <asp:Parameter Name="Antivirus" Type="String"/>
                    <asp:Parameter Name="Budget" Type="String"/>
               </UpdateParameters>

</asp:sqlDataSource>



<asp:gridview id="admgridview" runat="server" OnRowUpdating="Rowup" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" ForeColor="white" width="100%">
    <HeaderStyle ForeColor="#00bbff"/>
    <Columns>
    <asp:CommandField ShowEditButton="True" EditText="Modify" UpdateText="Update" CancelText="Cancel" />

        <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="true"/>
        <asp:BoundField DataField="PCUse" HeaderText="PCUse" ItemStyle-Width="18%"/>
        <asp:BoundField DataField="OS" HeaderText="Operating System" ItemStyle-Width="18%"/>
        <asp:BoundField DataField="ExtDev" HeaderText="External Devices" ItemStyle-Width="18%"/>
        <asp:BoundField DataField="Requests" HeaderText="Particular Requests" ItemStyle-Width="18%" />
        <asp:BoundField DataField="Antivirus" HeaderText="Antivirus" ItemStyle-Width="5%"/>
        <asp:BoundField DataField="Budget" HeaderText="Budget" ItemStyle-Width="5%" />
        <asp:BoundField DataField="State" HeaderText="State" ItemStyle-Width="13%" readonly="true"/>


    </Columns>
</asp:gridview>
</form>

以下是代码:

Dim strSQL As String
Dim index As Integer

Sub Page_Load(ByVal sender as Object, Byval e As EventArgs)

  If Session("log") = 1 Then
   login.innerHTML = "You are logged in as " & Session("user")
   change.href="./gtetrue.aspx"
   lout.innerHTML = "Log out"
  End If

  If Session("user") <> "Adm"
    Response.Redirect("./index.aspx")
  End if
End Sub

Sub sbout(ByVal sender as Object, Byval e As EventArgs)
    login.innerHTML = "You are not logged in at the moment"
    Session("log") = 0
    lout.innerHTML = "Sign in"
    Response.Redirect("./gtefalse.aspx")
End Sub

Sub admvis(Byval sender as Object, Byval e As EventArgs) 
    SqlDataSource1.SelectParameters.Add("status", Ddl1.SelectedValue.ToString())
    strSQL = "SELECT * FROM PCRequests WHERE State = [@status] Order By ID"
    SqlDataSource1.SelectCommand = strSQL
    admgridview.DataSourceID = "SqlDataSource1"
End Sub

Sub Rowup(Byval sender as Object, Byval e As EventArgs)
    Dim strSQL As String
    index = Convert.ToInt32(admgridview.Rows(admgridview.EditIndex).Cells(1).Text)
    strSQL = "UPDATE PCRequests SET PCUse = @PCUse, OS = @OS, ExtDev = @ExtDev, Requests = @Requests, Antivirus = @Antivirus, Budget = @Budget Where ID = " & index.ToString()
    SqlDataSource1.UpdateCommand = strSQL
End Sub

奇怪的是,我有一个类似于这个的页面但是没有'有'下拉列表来选择“状态”。 那个有效。

如果我将selectcommand直接放入“Sqldatasource1”并删除下拉列表以根据State选择数据库中的行,这个也有效。

先谢谢你。

0 个答案:

没有答案