I have a long table, so I want to split it into 3 GridView. Each GridView will have different headers (different columns from the DB). I want to use one SqlDataSource. For that, I am thinking to use a Repeater and then put 3 GridViews inside it.
Since I am using StoredProcedure that asks for one parameter in order to specify the headers of each GridView, I set the that parameter as HiddenField but even with that I did not get any result.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("GroupID")%>' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenField1" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" CellPadding="4"
DataKeyNames="BadgeNo" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT DISTINCT GroupID
from courses">
</asp:SqlDataSource>
In order to solve this problem you should have to plan or design the dataSource - may be you can use Linq or Entity Model or DataSet. I think the SqlDataSource is not appropriate here.
In current code-snippet, you should have to design SELECT - sql statement with where condition/clause of SqlDataSource for GridView controls.
Related
I'm trying to update GridView every time I update, delete or insert any record. Now I have provided sql data source code for GridView in Design Code not form code. Now how can I update it from there ? When I write GridView1.databind() on form code, it says
> Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Can someone tell me how to DataBind in design view, to update GridView every time I insert/update/delete record ?
Here is GridView1 Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" Width="284px">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
<asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myDbConnectionString %>" SelectCommand="SELECT [name], [surname], [amount] FROM [Table1]"></asp:SqlDataSource>
Set the DataSourceID to null.
GridView1.DataSourceID = null;
GridView1.DataSource = dt;
GridView1.DataBind();
That error was coming because data was being binded on both Design view code as well as web form backend code.
I simply added GridView1.DataSourceID = null;
and that did the job.
Let's take this code as example:
<div style="overflow:auto;width:700px;">
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateEditButton="True" DataMember="DefaultView"
DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
DataKeyNames="..." CellPadding="4" ForeColor="#333333" Width="90%"
Height="90%" Font-Size="Small">
<RowStyle BackColor="#EFF3FB" />
<Columns>.
.
.
<asp:CommandField DeleteText="delete" ShowDeleteButton="True"></asp:CommandField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBUserConnectionString %>"
SelectCommand="SELECT ... FROM ... "
DeleteCommand="DELETE FROM ... WHERE ...=#...;">
</asp:SqlDataSource>
Since I'm new to VB and ASP I'm using the sqlDatasource to fill the Gridview without VB code behind(please note this), everything is being shown in the aspx code. When I run it with browser and see the source code it only shows html and alot of javascript:__doPostBack's, no connection, no tables, not even asp controls.
However i'd like to know if this is the right way to fill griviews and if it is really safe, I mean is there a way see .aspx code on browser?
It's equally safe as putting it in code behind. However, you really should be putting this sort of thing in code behind so you can separate your logic from your view.
I don't generally put my SQL queries in codebehind as a matter of habit. I prefer to use webservices called by a Data Portal class which call stored procedures.
I'm unable to identify the problem which is causing the edit command to fail
GridView Code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="Requestid" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" Height="321px" Width="604px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" UpdateText="Submit" />
<asp:BoundField DataField="Requestid" HeaderText="Requestid" ReadOnly="True"
SortExpression="Requestid" />
<asp:BoundField DataField="Receiveddate" HeaderText="Receiveddate"
SortExpression="Receiveddate" />
<asp:BoundField DataField="Ctname" HeaderText="Ctname"
SortExpression="Ctname" />
<asp:TemplateField HeaderText="Requestor" SortExpression="Requestor">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="analystname"
DataValueField="contact_id" SelectedValue='<%# Bind("Requestor") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Customers %>" SelectCommand="SELECT [contact_id], [first_name]+ space(1)+ [last_name] as analystname FROM [contact]
order by contact_id"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Requestor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#CC3300" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#CBB06D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Customers %>" SelectCommand="SELECT RR.Requestid,Convert(varchar(20),RR.Receiveddate,100) as Receiveddate,RR.Ctname,C.First_Name +Space(1) + C.Last_NAME as Requestor from report_request_draft RR inner join Contact C on RR.requestor = C.contact_id
where clonedfromid is not null"
UpdateCommand="UPDATE [REPORT_REQUEST_DRAFT] SET [REQUESTOR] = #Requestor WHERE [requestid] = #requestid">
</asp:SqlDataSource>
When i click on the edit command button it gives me the below error
Error
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The DropDownList can't be bound in this way. By using the SelectedValue binding as you have it, it requires that the DropDownList is bound before the GridView is bound, but that is a logical impossibility since the DropDownList can't be bound until the GridView has completed its binding. You should put this logic in the code behind with the RowDataBound event. At that point, you should manually bind your DropDownList and then set its SelectedValue property.
Consider the following link which will help you to solve your problem and it also shows you how to bind EditItemTemplet perfectly in grid View. I condenser you to use this link for better understand grid view.
Hi
i am trying to create an insertion form in ASP.net.
but i get this exception and i could not find this with google.
what am i doing wrong ?
here is my code:
<div id = "survey_div">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:surveyConnectionString1 %>"
DeleteCommand="DELETE FROM survey"
InsertCommand="INSERT INTO survey(age, country, city) VALUES (,,)"
SelectCommand="SELECT FROM survey" UpdateCommand="UPDATE survey SET">
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4"
DataSourceID="SqlDataSource1" EnableModelValidation="True" ForeColor="#333333"
GridLines="None" Height="50px" Width="125px">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<Fields>
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
</asp:DetailsView>
</div>
It looks like it's your SelectCommand, you need to either have a list of columns, or * between Select and From (preferably a list of columns).
It looks like the SelectCommand in your SqlDataSource is not specifying any columns to select:
SELECT FROM survey
I have a gridview where, if you select a row, it takes you to a formview where you can view details from that entry and edit the data. This worked when I had only one filter parameter, but I added another, and now the formview is showing up blank. Here's my code:
<asp:AccessDataSource ID="courseinfo" runat="server" DataFile="~/App_Data/courseinfo.mdb" SelectCommand="SELECT [prefix], [course_number], [title], [date_submitted] FROM [tableCourse]">
</asp:AccessDataSource>
<asp:AccessDataSource ID="courseinfo2" runat="server" DataFile="~/App_Data/courseinfo.mdb" FilterExpression="prefix='{0}' AND course_number='{1}'" SelectCommand="SELECT * FROM [tableCourse]">
<FilterParameters>
<asp:ControlParameter Name="prefix" ControlID="GridView1" PropertyName="SelectedValue" />
<asp:ControlParameter Name="course_number" ControlID="GridView1" PropertyName="SelectedValue" />
</FilterParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="courseinfo" DataKeyNames="prefix,course_number"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False"
CellPadding="8" ForeColor="#333333" GridLines="vertical" BorderWidth="1" BorderColor="#dddddd"
AllowPaging="true" AllowSorting="true" AutoGenerateSelectButton="true">
<RowStyle BackColor="#eeeeee" />
<Columns>
<asp:BoundField DataField="prefix" HeaderText="prefix" SortExpression="prefix" />
<asp:BoundField DataField="course_number" HeaderText="number" SortExpression="course_number" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="date_submitted" HeaderText="date submitted" SortExpression="date_submitted" />
</Columns>
<FooterStyle BackColor="black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="black" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#c0c0e0" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="#eeeeee" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" DataSourceID="courseinfo2"
DataKeyNames="prefix,course_number" Width="100%">
Of course the formview is complete but it contains a lot of code so I didn't include that here, although if anyone wants to see it, I can post it.
You can change your control parameters as follows reason being since you have multiple datakeynames, you cant have selected value as property of both. This should work however:
<asp:ControlParameter Name="prefix" ControlID="GridView1" PropertyName="SelectedDataKey.Values[prefix]" Type="String" />
<asp:ControlParameter Name="course_number" ControlID="GridView1" PropertyName="SelectedDataKey.Values[course_number]" Type="Int32" />