Reputation: 21
I would like to know how I can connect a single GridView
to two SqlDataSource
objects?
At the moment I experience an error stating that both DataSource and DataSourceID are defined on 'GridView1', so I removed one definition. But I dont want remove any of the definitions, since both are important.
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# Code
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();
}
Upvotes: 0
Views: 1789
Reputation: 21
Simpliy on every click event instead of GridView1.DataSource = SqlDataSource2
Change ...
protected void btnFindName_Click(object sender, EventArgs e)
{
GridView1.DataSourceID = "SqlDataSource2";
GridView1.DataBind();
}
Upvotes: 1
Reputation: 17614
The error is Both DataSource and DataSourceID
is clear. The problem is in you html of Gridview
.
Remove one of the property either DataSource
or DataSourceID
.
Similar Question on SO
Or you try to bind data to data grid using sqldatasource
from design side as well as using c# code from code behind side..
<asp:gridview runat="server" autogeneratecolumns="False"
DataSourceID="Datasource1">
Here DataSourceID
is specified and in code behind GridView1.DataSource = SqlDataSource2;
is causing the problem.
Upvotes: 0
Reputation: 9414
You must join 2 table in one SQLDatasource.like this:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Upvotes: 0