Dropdownlist in templatefield - update fails (null value insertion) - asp.net

I have a gridview. In this i have a templatefield with a DropDownList (DDL in EditItemTemplate mode, label in ItemTemplate mode).
When i hit edit on of of the detailsview's rows i can select any value from the DDL (the DDL is populated from a sqldatasource), but if i try to execute the update, it fails, because it thinks i didn't supply the data...
Here is the exact error (the DB refuse NULL data):
Cannot insert the value NULL into column 'status', table
'gyumolcs.dbo.orders'; column does not allow nulls. UPDATE fails. The
statement has been terminated.
Here is the code for the gridview:
<!-- language: c# -->
<asp:SqlDataSource ID="orderadminSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date"
DeleteCommand="DELETE FROM orders WHERE (ID = #ID)"
UpdateCommand="UPDATE orders SET status = #status WHERE (ID = #ID)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="orderadminSqlDataSource" DataKeyNames="ID" Width="608px"
AllowPaging="True" AllowSorting="True" PageSize="15">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True" >
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="Felhasználónév"
SortExpression="UserName" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Mennyiség (kg)"
SortExpression="Quantity" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Product" HeaderText="Termék"
SortExpression="Product" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Rendelés státusz" SortExpression="status">
<EditItemTemplate>
<!--<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>-->
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="statustypeDDLSqlDataSource" DataTextField="name"
DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="statustypeDDLSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [status]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Date" HeaderText="Dátum" SortExpression="Date" />
</Columns>
<PagerSettings PageButtonCount="15" Mode="NumericFirstLast" />
</asp:GridView>
And here is the orderadminSqlDataSource's code (the gridview's datasource)
<!-- language: c# -->
<asp:SqlDataSource ID="orderadminSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date"
DeleteCommand="DELETE FROM orders WHERE (ID = #ID)"
UpdateCommand="UPDATE orders SET status = #status WHERE (ID = #ID)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
Please help me, i can't figure out the problem.
Thanks in advance!

I think you're forgetting to set the DropDown's SelectedValue property:
<asp:DropDownList ID="DropDownList1"
runat="server"
DataSourceID="statustypeDDLSqlDataSource"
DataTextField="name"
DataValueField="status"
SelectedValue='<%# Bind("status") %>'
AppendDataBoundItems="True" >
Change your code for statustypeDDLSqlDataSource:
<asp:SqlDataSource ID="statustypeDDLSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT [ID] as status, [name] FROM [status]">
</asp:SqlDataSource>
You can also simplify this ( no parenthesis necessary ):
UPDATE orders SET [status] = #status WHERE [ID] = #ID
Note: I had to rename the ID column in the query so that it conforms with the status parameter you defined here:
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
My last edit and attempt here :D
After carefully looking at your GridView's SELECT statement I see where the error lies:
SELECT orders.ID,
aspnet_Users.UserName,
orders.quantity AS Quantity,
product.name AS Product,
status.name AS Status,
orders.date AS Date
FROM orders
INNER JOIN product ON
orders.ordertype = product.ID
INNER JOIN status ON orders.status = status.ID
INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId
ORDER BY date
Change it to:
SELECT orders.ID,
aspnet_Users.UserName,
orders.quantity AS Quantity,
product.name AS Product,
status.ID AS status,
status.name AS StatusName,
orders.date AS Date
FROM orders
INNER JOIN product ON
orders.ordertype = product.ID
INNER JOIN status ON orders.status = status.ID
INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId
ORDER BY date
Now you must change this part too:
<ItemTemplate>
<asp:Label ID="status" runat="server" Text='<%# Bind("StatusName") %>'></asp:Label>
</ItemTemplate>
By the way... here's a nice step by step tutorial on this matter:
Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control

Related

ASP VB - How to update a database where the selected item is from a dropdown list using a datasource

Hi I am new to ASP and was able to create a dropdown list which list all the items from my table (DB2_INSTANCES).
Now I need to update a second table (PROCESS) with the value selected DB2_DSN. The table does not allow Null values.
If I use "SelectedValue='<%# Bind("DB2_DSN") %>' within my asp:DropDownList after I set the
DataSourceID="SqlDataSource6" DataTextField="INSTANCE" DataValueField="INSTANCE"
I get an error:
"'DropDownList6' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value "
If I change to "SelectedValue='<%# Bind("INSTANCE") %>' within my asp:DropDownList after I set the
DataSourceID="SqlDataSource6" DataTextField="INSTANCE" DataValueField="INSTANCE"
I get an error:
"DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'INSTANCE'."
If I remove the "SelectedValue..." I get the error:
Cannot insert the value NULL into column 'DB2_DSN', table 'xxx.dbo.PROCESS'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
I also tried using the DataBind() but it also gives me an error.
The other value (FTP_IND) works fine because it is listed/hardcoded.
Why does it work for FTP_IND and not for DB2_DSN?
First:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxConnectionString2 %>"
DeleteCommand="DELETE FROM PROCESS WHERE NAME = #NAME AND ENVIRONMENT = #ENVIRONMENT"
SelectCommand="SELECT * FROM [PROCESS]"
UpdateCommand="UPDATE PROCESS SET ENVIRONMENT = #ENVIRONMENT, PROCESS_STATUS = #PROCESS_STATUS, IC_VALUE = #IC_VALUE,
FTP_IND = #FTP_IND, DB2_DSN = #DB2_DSN, DB2_REGION = #DB2_REGION,
ALERT_EMAIL = #ALERT_EMAIL, NOTIFY_EMAIL = #NOTIFY_EMAIL, DEBUG_INFO = #DEBUG_INFO,
LAST_UPDATE_DATE = CURRENT_TIMESTAMP, LAST_UPDATE_USERID = #UID, LAST_UPDATE_IP = #UIP WHERE (NAME = #NAME)">
<UpdateParameters>
<asp:SessionParameter Name="UID" SessionField="User_ID" Type="String" />
<asp:SessionParameter Name="UIP" SessionField="User_IP" Type="String" />
<asp:Parameter Name="ENVIRONMENT" />
<asp:Parameter Name="PROCESS_STATUS" />
<asp:Parameter Name="IC_VALUE" />
<asp:Parameter Name="FTP_IND" />
<asp:Parameter Name="DB2_DSN" />
<asp:Parameter Name="DB2_REGION" />
<asp:Parameter Name="ALERT_EMAIL" />
<asp:Parameter Name="NOTIFY_EMAIL" />
<asp:Parameter Name="DEBUG_INFO" />
<asp:Parameter Name="NAME" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxConnectionString2 %>"
SelectCommand="SELECT [INSTANCE] FROM [DB2_INSTANCES] ORDER BY [INSTANCE] ASC">
</asp:SqlDataSource>
Second:
<asp:TemplateField HeaderText="FTP" SortExpression="FTP_IND">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server"
SelectedValue='<%# Bind("FTP_IND") %>'
ToolTip="Use this to turn off and on the FTP process" Font-Size="Small">
<asp:ListItem>N</asp:ListItem>
<asp:ListItem>Y</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("FTP_IND") %>'
ToolTip="Indicates if FTP will take place" Font-Size="Small">
</asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="Small" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText = "DB2 Instance" SortExpression="DB2_DSN">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server"
ToolTip="Use this to choose the DB2 Instance" Font-Size="Small"
DataSourceID="SqlDataSource6"
DataTextField="INSTANCE"
DataValueField="INSTANCE">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("DB2_DSN") %>'
ToolTip="Indicates the DB2 Instance being used" Font-Size="Small">
</asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="Small" HorizontalAlign="Center" />

ASP.NET Filter Access Database Gridview with Dropdownlist

I am trying to filter a gridview with a dropdownlist. My gridview and dropdownlist are being populated by an Access database. What I want is when a supplier is selected from the dropdownlist it will filter the gridview of products to those with that supplierid.
I am able to populate the dropdownlist and gridview, but when I select a Supplier nothing happens.
I tried adding FilterExpression="SupplierID Like '{0}%'", but then the page errors out with Cannot perform 'Like' operation on System.Int32 and System.String
Here is my code.
<form id="form" runat="server" style="margin:0 auto;">
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="AccessDataSource2" DataTextField="Supplier" DataValueField="ID" AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text="All" Value="%" />
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/Hovden Oil Pricing.accdb"
SelectCommand="SELECT [Supplier], [ID] FROM [Suppliers] ORDER BY [Supplier]">
</asp:AccessDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="White"
DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
SortExpression="SupplierID" />
<asp:BoundField DataField="Product Number" HeaderText="Product Number"
SortExpression="Product Number" />
<asp:BoundField DataField="Product Name" HeaderText="Product Name"
SortExpression="Product Name" />
<asp:BoundField DataField="Product Type" HeaderText="Product Type"
SortExpression="Product Type" />
<asp:CheckBoxField DataField="Bulked" HeaderText="Bulked"
SortExpression="Bulked" />
<asp:BoundField DataField="UOM" HeaderText="UOM" SortExpression="UOM" />
<asp:BoundField DataField="Multiple" HeaderText="Multiple"
SortExpression="Multiple" />
<asp:BoundField DataField="$/GAL" HeaderText="$/GAL" SortExpression="$/GAL" />
<asp:BoundField DataField="$/UNIT" HeaderText="$/UNIT" ReadOnly="True"
SortExpression="$/UNIT" />
<asp:BoundField DataField="TypeID" HeaderText="TypeID"
SortExpression="TypeID" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Hovden Oil Pricing.accdb" SelectCommand="SELECT [Supplier Products].SupplierID, Products.[Product Number], Products.[Product Name], [Product Types].[Product Type], Products.Bulked, Products.UOM, Products.Multiple, [Bulk Prices].Volume AS [$/GAL], [$/GAL]*[Multiple] AS [$/UNIT], [Product Types].TypeID
FROM [Product Types] INNER JOIN ((Products INNER JOIN [Supplier Products] ON Products.[Product Number] = [Supplier Products].[Product Number]) INNER JOIN [Bulk Prices] ON (Products.[Product Number] = [Bulk Prices].[Product Number]) AND (Products.[Product Number] = [Bulk Prices].[Product Number])) ON [Product Types].TypeID = Products.[Product Type]
WHERE (Products.Bulked)=Yes
ORDER BY Products.[Product Name];" FilterExpression="SupplierID Like '{0}%'">
<FilterParameters>
<asp:ControlParameter Name="SupplierID" ControlID="DropDownList1" PropertyName="SelectedValue" />
</FilterParameters>
</asp:AccessDataSource>
</form>
Am I missing something?
Thanks.
It's quite simple really, you are trying to compare an int(SupplierID) to a wild card string value, that's why you're getting the above error.
Simply convert the SupplierID to a string and it will work:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Hovden Oil Pricing.accdb" SelectCommand="SELECT [Supplier Products].SupplierID, Products.[Product Number], Products.[Product Name], [Product Types].[Product Type], Products.Bulked, Products.UOM, Products.Multiple, [Bulk Prices].Volume AS [$/GAL], [$/GAL]*[Multiple] AS [$/UNIT], [Product Types].TypeID
FROM [Product Types] INNER JOIN ((Products INNER JOIN [Supplier Products] ON Products.[Product Number] = [Supplier Products].[Product Number]) INNER JOIN [Bulk Prices] ON (Products.[Product Number] = [Bulk Prices].[Product Number]) AND (Products.[Product Number] = [Bulk Prices].[Product Number])) ON [Product Types].TypeID = Products.[Product Type]
WHERE (Products.Bulked)=Yes
ORDER BY Products.[Product Name];" FilterExpression="Convert(SupplierID , 'System.String') LIKE '{0}%'">
<FilterParameters>
<asp:ControlParameter Name="SupplierID" ControlID="DropDownList1" PropertyName="SelectedValue" />
</FilterParameters>

Gridview not updating all fields

I have a gridview that is filled correctly from 3 tables.
When I update it, it will only update one field (Kommentar) but only the first time.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID,Erledigt,Kommentar"
DataSourceID="ArtikelDataSource">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
ReadOnly="True" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Beschreibung" HeaderText="Beschreibung"
SortExpression="Beschreibung" ReadOnly="True" >
<ControlStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="Datum" HeaderText="Datum" SortExpression="Datum"
ReadOnly="True" >
<ControlStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Erledigt" HeaderText="Erledigt"
SortExpression="Erledigt" >
<ControlStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Kommentar" HeaderText="Kommentar"
SortExpression="Kommentar" >
<ControlStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="LastUpdate" HeaderText="LastUpdate"
SortExpression="LastUpdate" ReadOnly="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" Visible="False" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="ArtikelDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT ci.Name, ci.Beschreibung, ca.ChkItemID, ca.ChkUserID, ca.Datum, ca.Erledigt, ca.Kommentar, ca.LastUpdate, ca.ID FROM ChecklistActions AS ca INNER JOIN ChecklistItems AS ci ON ca.ChkItemID = ci.ID INNER JOIN ChecklistUsers AS cu ON ca.ChkUserID = cu.ID WHERE (ca.Datum = #datum) AND (cu.Login = #userlogin)"
UpdateCommand="UPDATE ChecklistActions SET Kommentar = #Kommentar, LastUpdate = GETDATE() WHERE (ID = #ID)"
OnSelecting="_data_Selecting"
>
<SelectParameters>
<asp:Parameter Name="userlogin"/>
<asp:Parameter Name="datum"/>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Kommentar" Type="string"/>
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
I have first an empty field "Kommentar", then it is filled, but when I try to change the value again, only the GETDATE() changes value, the content of Kommentar is the same as before.
When I try to change the value of "Erledigt" via SQL
UpdateCommand="UPDATE ChecklistActions SET Erledigt = #Erledigt, Kommentar = #Kommentar, LastUpdate = GETDATE() WHERE (ID = #ID)"
...
<asp:Parameter Name="Erledigt" Type="Int32"/>
nothing (except the Getdate()) is updated at all - not even the field Kommentar at the fist time
It seems that the gridview is sending the old values to the update command (only the first update will receive the new Kommentar-value) which is really irritating ?
you missed , in your UpdateCommand
UPDATE ChecklistActions SET Erledigt = #Erledigt, Kommentar = #Kommentar, LastUpdate = GETDATE() WHERE (ID = #ID)"
edit
Set only the primary key column names on your database tables as DataKeyNames, in this case you only update ChecklistActions table. so set primary key of ChecklistActions as DataKeyNames

Error trying to filter drop down list with a Gridview field value via a controlparameter

I'm getting the dreaded 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value error when trying to filter a drop down list in a templatefield using one of the other boundfield values (I'm trying to get a list of employees based on their department - i.e. the user can change the employee but only to another member of the same department).
Here's the code:
<asp:GridView ID="Rotas" runat="server" AllowSorting="True"
DataSourceID="SqlDataSource3" AutoGenerateEditButton="True" DataKeyNames="DateFrom,DateTo,DepartmentId"
AutoGenerateColumns="False" OnRowUpdating="Rotas_RowUpdating">
<Columns>
<asp:BoundField DataField="DateFrom" HeaderText="DateFrom" ReadOnly="True"
SortExpression="DateFrom" />
<asp:BoundField DataField="DateTo" HeaderText="DateTo" ReadOnly="True"
SortExpression="DateTo" />
<asp:TemplateField HeaderText="Employee Name" SortExpression="EmployeeName">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource4" DataTextField="EmployeeName"
DataValueField="EmployeeName" SelectedValue='<%# Bind("EmployeeName") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DepartmentId" HeaderText="DepartmentId"
ReadOnly="True" SortExpression="DepartmentId" />
<asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" ReadOnly="False"
SortExpression="EmployeeId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
SelectCommand="SELECT r.DateFrom, r.DateTo, e.EmployeeName, e.EmployeeId, r.departmentid
FROM
dbo.[Rota] r INNER JOIN
dbo.[Employee] AS e ON r.EmployeeId = e.EmployeeId
WHERE (r.DateTo >= GETDATE()) "
UpdateCommand="UPDATE [Rota] SET [EmployeeId] = (select employeeid from employee where employeename = #EmployeeName),
[departmentid] = (select departmentid from employee where employeename = #EmployeeName)
WHERE [DateFrom] = #DateFrom AND [DateTo] = #DateTo AND [DepartmentId] = #DepartmentId">
<UpdateParameters>
<asp:Parameter Name="DateTo" Type="DateTime" />
<asp:Parameter Name="DateFrom" Type="DateTime" />
<asp:Parameter Name="DepartmentId" Type="Int16" />
<asp:Parameter Name="EmployeeId" Type="Int16" />
<asp:Parameter Name="EmployeeName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
onselecting="SqlDataSource4_Selecting"
SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=#DepartmentId"
>
<SelectParameters>
<asp:ControlParameter ControlID="Rotas" Name="DepartmentId"
PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Really can't see what I'm doing wrong. If I don't use the Select Parameter and just a 'select employeename from employee' then the whole list of employees is displayed fine. As soon as I try and use a controlparameter it falls over. Help! :)
Thanks in advance for any assistance offered.
I think the problem is that your SqlDataSource that returns the list of employees for a department isn't returning any rows, and I think this is because the ControlParameter isn't right. Although you've done the right thing by pointing it at the Rotas GridView and the SelectedValue property, there are three fields used in the DataKeyNames property (DateFrom, DateTo, DepartmentId), and I believe you're currently passing the DateFrom value into your query - hence, no results.
What I think you need to use in the PropertyName of the ControlParameter instead of the SelectedValue property of Rotas is the SelectedDataKey - there's details on MSDN here although the demo code there isn't particularly useful. However the important line is:
If you are creating a ControlParameter
object and want to access a key field
other than the first field, use the
indexed SelectedDataKey property in
the PropertyName property of the
ControlParameter object
So from that I think what you need is:
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
onselecting="SqlDataSource4_Selecting"
SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=#DepartmentId"
>
<SelectParameters>
<asp:ControlParameter ControlID="Rotas" Name="DepartmentId"
PropertyName="SelectedDataKey[2]" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Without a copy of your data I can't test it, but give it a go and see what you get...

Multiple DataKeyNames in Master Detail Gridview

This code lets you search for a table name in an Oracle database. then when you select a row it puts all the columns for the selected table in the second gridview.
The Code below works if I only use one DatakeyName - "Table_Name" but it will display identical table names with different owners if they exist which is not what I want. I want to pull the details based on two fields - Table_Name and Owner.
I can't figure out how to get the details part to work with two datakeyNames.
<asp:Label ID="lblTitleSrchOracleTab" runat="server" Text="Search For A Table In Oracle"></asp:Label>
<br /><br />
<asp:Label ID="lblOracleTableName" runat="server" Text="Oracle Table Name"></asp:Label>
<asp:TextBox ID="txtOracleTableName" runat="server"></asp:TextBox>
<asp:Button ID="btnOracleTableName" runat="server" Text="Search"
/>
<br /><br />
<asp:GridView ID="gvOracleTableName" runat="server" CssClass="mGrid"
AutoGenerateSelectButton="True" AutoGenerateColumns="False"
DataSourceID="sdsOracleTableName" DataKeyNames="Owner,Table_Name" >
<Columns>
<asp:BoundField DataField="OWNER" HeaderText="OWNER" SortExpression="OWNER" />
<asp:BoundField DataField="TABLE_NAME" HeaderText="TABLE_NAME"
SortExpression="TABLE_NAME" />
<asp:BoundField DataField="NUM_ROWS" HeaderText="NUM_ROWS"
SortExpression="NUM_ROWS" />
<asp:BoundField DataField="TABLESPACE_NAME" HeaderText="TABLESPACE_NAME"
SortExpression="TABLESPACE_NAME" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsOracleTableName" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT Owner, Table_name, Num_Rows, Tablespace_name
FROM all_tables
WHERE trim(upper(table_name)) LIKE trim(upper('%' || :TableName || '%'))">
<SelectParameters>
<asp:ControlParameter ControlID="txtOracleTableName" Name="TableName"
PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<br /><br />
<asp:GridView ID="gvSelectedTableColumns" runat="server" CssClass="mGrid"
AutoGenerateColumns="False" DataSourceID="sdsgvSelectedTableColumns">
<Columns>
<asp:BoundField DataField="OWNER" HeaderText="OWNER" SortExpression="OWNER" />
<asp:BoundField DataField="TABLE_NAME" HeaderText="TABLE_NAME"
SortExpression="TABLE_NAME" />
<asp:BoundField DataField="COLUMN_NAME" HeaderText="COLUMN_NAME"
SortExpression="COLUMN_NAME" />
<asp:BoundField DataField="DATA_TYPE" HeaderText="DATA_TYPE"
SortExpression="DATA_TYPE" />
<asp:BoundField DataField="DATA_LENGTH" HeaderText="DATA_LENGTH"
SortExpression="DATA_LENGTH" />
<asp:BoundField DataField="NULLABLE" HeaderText="NULLABLE"
SortExpression="NULLABLE" />
</Columns>
<SelectedRowStyle BorderColor="Red" Font-Bold="True" />
</asp:GridView>
<asp:SqlDataSource ID="sdsgvSelectedTableColumns" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT Owner, table_name, column_name, Data_Type, Data_Length, Nullable
FROM all_tab_columns
WHERE trim(upper(Owner)) =trim(upper(:SelectedOwner)) AND
trim(upper(table_name)) =trim(upper(:SelectedTableName))
">
<SelectParameters>
<asp:ControlParameter ControlID="gvOracleTableName" Name="SelectedOwner"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="gvOracleTableName" DefaultValue=""
Name="SelectedTableName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Can't you extend the where clause?
So
from all_tables where ... and owner = 'MYOWNER'
or
select from user_tables instead of all_tables.
that's what this is
WHERE trim(upper(Owner)) =trim(upper(:SelectedOwner)) AND
trim(upper(table_name)) =trim(upper(:SelectedTableName))
my problem is that when you use the sqlDataSource wizard to connect the paramters to the gridview control, it doesn't let you select which datakey of the gridview to bind it to.
I finally figured this one out. You can't Mighty Mouse it - it takes actual code! Specifically the gridview_SelectedIndexChanged event.
Here's how I did it.
protected void gvOracleTableName_SelectedIndexChanged(object sender, EventArgs e)
{
string SelectedOwner;
SelectedOwner = gvOracleTableName.SelectedRow.Cells[1].Text ;
string SelectedTableName;
SelectedTableName = gvOracleTableName.SelectedRow.Cells[2].Text;
lblTest.Text = SelectedOwner + " " + SelectedTableName;
string strConn, strSQL;
strConn = #"";
strSQL = #"SELECT Owner, table_name, column_name, Data_Type, Data_Length, Nullable FROM all_tab_columns WHERE trim(upper(Owner)) =trim(upper(:SelectedOwner)) AND trim(upper(table_name)) =trim(upper(:SelectedTableName))";
using (OracleConnection cn = new OracleConnection(strConn))
{
OracleCommand cmd = new OracleCommand(strSQL, cn);
cmd.Parameters.AddWithValue(":SelectedOwner", SelectedOwner);
cmd.Parameters.AddWithValue(":SelectedTableName", SelectedTableName);
cn.Open();
OracleDataReader rdr = cmd.ExecuteReader();
gvSelectedTableColumns.DataSource = rdr;
gvSelectedTableColumns.DataBind();
cn.Close();
}
}

Resources