如何在C#中将GridView连接到两个不同的sqldatasource

时间:2014-09-15 06:31:44

标签: c# asp.net gridview ado.net

我想知道如何将单个GridView连接到两个SqlDataSource个对象?

目前我遇到一个错误,指出DataSource和DataSourceID都是在GridView1'上定义的,所以我删除了一个定义。但我不想删除任何定义,因为两者都很重要。

Aspx Markup

SqlDataSource1

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DBCS %>" 
                DeleteCommand="DELETE FROM [vendor_master] WHERE [vendorID] = @vendorID" 
                InsertCommand="INSERT INTO [vendor_master] ([Vname], [Email], [Mobile], [Landline], [Address], [Pincode]) VALUES (@Vname, @Email, @Mobile, @Landline, @Address, @Pincode)" 
                SelectCommand="SELECT * FROM [vendor_master]" 
                UpdateCommand="UPDATE [vendor_master] SET [Vname] = @Vname, [Email] = @Email, [Mobile] = @Mobile, [Landline] = @Landline, [Address] = @Address, [Pincode] = @Pincode WHERE [vendorID] = @vendorID">
                <DeleteParameters>
                    <asp:Parameter Name="vendorID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="Vname" Type="String" />
                    <asp:Parameter Name="Email" Type="String" />
                    <asp:Parameter Name="Mobile" Type="Int64" />
                    <asp:Parameter Name="Landline" Type="Int64" />
                    <asp:Parameter Name="Address" Type="String" />
                    <asp:Parameter Name="Pincode" Type="Int32" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Vname" Type="String" />
                    <asp:Parameter Name="Email" Type="String" />
                    <asp:Parameter Name="Mobile" Type="Int64" />
                    <asp:Parameter Name="Landline" Type="Int64" />
                    <asp:Parameter Name="Address" Type="String" />
                    <asp:Parameter Name="Pincode" Type="Int32" />
                    <asp:Parameter Name="vendorID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>

SqlDataSource2

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DBCS %>" SelectCommand="FindVendor" 
                SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:ControlParameter ControlID="txtFindVendor" Name="name" PropertyName="Text" 
                        Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>

C#代码

protected void btnAddVendor_Click(object sender, EventArgs e)
{
    if (IsValid)
    {
        SqlDataSource1.InsertParameters["Vname"].DefaultValue = txtName.Text;
        SqlDataSource1.InsertParameters["Email"].DefaultValue = txtEmail.Text;
        SqlDataSource1.InsertParameters["Mobile"].DefaultValue = txtMobile.Text;
        SqlDataSource1.InsertParameters["Landline"].DefaultValue = txtLandline.Text;
        SqlDataSource1.InsertParameters["Address"].DefaultValue = txtAddress.Text;
        SqlDataSource1.InsertParameters["Pincode"].DefaultValue = txtPincode.Text;

        SqlDataSource1.Insert();
    }
}

protected void btnFindName_Click(object sender, EventArgs e)
{
    GridView1.DataSource = SqlDataSource2;
    GridView1.DataBind();
}

3 个答案:

答案 0 :(得分:1)

对每次点击活动进行简化,而不是GridView1.DataSource = SqlDataSource2更改...

protected void btnFindName_Click(object sender, EventArgs e)
    {
        GridView1.DataSourceID = "SqlDataSource2";
        GridView1.DataBind();
    }

答案 1 :(得分:0)

您必须在一个SQLDatasource中加入2个表。这样:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
  FROM Orders
  INNER JOIN Customers
  ON Orders.CustomerID=Customers.CustomerID; 

答案 2 :(得分:0)

错误Both DataSource and DataSourceID很明显。问题出在你的Gridview

的html中

删除其中一个属性DataSourceDataSourceID

关于SO的类似问题

  1. Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition
  2. 编辑1

    或者您尝试使用设计端的sqldatasource将数据绑定到数据网格,以及使用代码背后的c#代码。

    <asp:gridview  runat="server" autogeneratecolumns="False" 
         DataSourceID="Datasource1">
    

    此处指定DataSourceIDGridView1.DataSource = SqlDataSource2;后面的代码导致问题。