I'm having problems passing a null value to a stored procedure, e.g. if an option isn't selected on a dropdown how can this be passed as null to through the sqdatasource to the database.
I currently have a number of dropdown controls on my page which are holding paramater values to get passed to a stored procedure.
When I select some values from these controls the gridview will display the results that it should, but what I'm having a problem with is when no values are selected is passing a null value to the SP. I've checked the SP and when I execute it and pass in null values it gets me the correct results so I'm happy with the SP. I've tried
ConvertEmptyStringToNull="true" DefaultValue=""
settings in the control paramater with no luck, and the dropdown's "ALL" option has a value of ""
The code for the sqldatasource is:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:scConnString %>"
SelectCommand="spGetOrgTickets" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:SessionParameter Name="org_id" Type="Int32" SessionField="org_id" DefaultValue="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="drpPriority" Name="priority_id" PropertyName="SelectedValue"
Type="Int32" ConvertEmptyStringToNull="true" DefaultValue="" />
<asp:ControlParameter ControlID="drpStatus" Name="status_id" PropertyName="SelectedValue"
Type="Int32" ConvertEmptyStringToNull="true" DefaultValue=""/>
</SelectParameters>
</asp:SqlDataSource>
One of the dropdown's is :
<asp:DropDownList Style="width: 100%" ID="drpStatus" runat="server" class="field select"
AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2_Status"
DataTextField="status" DataValueField="status_id">
<asp:ListItem Value="">All</asp:ListItem>
</asp:DropDownList>
Any ideas?
Thanks
Sorted...just need CancelSelectOnNullParameter="false" within sqldatasource.
Related
How do you set a parameter passed to a SqlDataSource to be null? I have the code below where the parameter "#var1" is set from a TextBox. However, if that value is not defined I need to set "#var1" to be null so that the coalesce returns true.
Is there a way to do this or am I going about it the wrong way?
I've also tried setting the DefaultValue="null" and dsABC.SelectParameters["#var1"]=null which both lead to "Input string was not in a correct format" errors.
<asp:SqlDataSource ID="dsABC" runat="server" ConnectionString="<%$ ConnectionStrings:abc123 %>"
SelectCommand=" SELECT col1, col2, col3
FROM table1 as T1
WHERE COALESCE(#var1, T.ID) = T.ID">
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Add the ConvertEmptyStringToNull and the CancelSelectOnNullParameter attributes. The first sets the parameter to null when empty, the second lets the query run (default behavior is to not run a query with a null parameter).
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16"
ConvertEmptyStringToNull="true"
CancelSelectOnNullParameter="false" />
</SelectParameters>
I'm getting a "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." error message. I've looked here to see what others have said about that error. It looks to me as though some people have gotten the binding syntax wrong. I believe I am using the correct data binding, but I could be wrong. The error occurs on the first line of the DropDownList. I'm using VS 2015.
<asp:DropDownList runat="server" ID="ddlUsersRating" AutoPostBack="True"
DataSourceID="sdsUserRatings" DataTextField="RatingID"
DataValueField="RatingID" Height="16px"
SelectedValue='<%# Bind("RatingID") %>'
AppendDataBoundItems="False"
OnSelectedIndexChanged="ddlUsersRating_SelectedIndexChanged">
<asp:ListItem Value="">(Select a Rating)</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource runat="server" ID="sdsUserRatings"
ConnectionString="<%$ ConnectionStrings:VideoLibraryConnectionString %>"
InsertCommand="INSERT INTO UserRatings(UserID, ShowID, RatingID) VALUES (#UserID, #ShowID, #RatingID)"
SelectCommand="SELECT UserRatings.RatingID FROM UserRatings INNER JOIN Users ON UserRatings.UserID = Users.UserID WHERE (UserRatings.ShowID = #ShowID) AND (Users.UserID = #UserID)"
UpdateCommand="UPDATE UserRatings SET RatingID = #RatingID WHERE (UserID = #UserID) AND (ShowID = #ShowID)" OnInserted="sdsUserRatings_Inserted">
<InsertParameters>
<asp:ControlParameter ControlID="ddlUsers" Name="UserID"
PropertyName="SelectedValue" />
<asp:RouteParameter Name="ShowID" RouteKey="ShowID" />
<asp:ControlParameter ControlID="ddlUsersRating" Name="RatingID"
PropertyName="SelectedValue" />
</InsertParameters>
<SelectParameters>
<asp:RouteParameter Name="ShowID" RouteKey="ShowID" />
<asp:ControlParameter ControlID="ddlUsers" Name="UserID"
PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter ControlID="ddlUsersRating" Name="RatingID"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ddlUsers" Name="UserID"
PropertyName="SelectedValue" />
<asp:RouteParameter Name="ShowID" RouteKey="ShowID" />
</UpdateParameters>
</asp:SqlDataSource>
This error means you can only use Bind, Eval, XPath if your object is DataBound meaning it has DataSource.
As per seeing your DropDownList you have embedded static values in it Blank, 1 - 5.
Remove this property from your DropDownList : SelectedValue='<%# Bind("RatingID") %>'
Then to do this..
On one of your form events, let's say Load
Protected Sub Page_Load()
Dim RatingID = (some select query here to get RatingID)
ddlUserRating.ClearSelection()
ddlUserRating.Items.FindByValue(RatingID).Selected = True
End Sub
<asp:SqlDataSource ID="HopefulDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand= "SELECT id, regGroupID, amountReceived, other FROM table"
UpdateCommand="UPDATE table
SET [amountReceived] = #amountReceived
WHERE [regGroupID] = #regGroupID">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCourses" Name="ddlSelectedCourse" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="regGroupID" Type="Int32" />
<asp:Parameter Name="amountReceived" Type="Decimal" />
other parameters
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
The above works when I change "WHERE [regGroupID] = #regGroupID" to either
WHERE [id] = #id
or
WHERE [regGroupID] = 2
You need to add "regGroupID" to the DataKeyNames collection in your GridView declaration. Something like this:
<asp:GridView ID="yourGridViewId" DataKeyNames="regGroupID" ... >
...
</asp:GridView>
See the DataKeyNames documentation:
You must set the DataKeyNames property in order for the automatic
update and delete features of the GridView control to work. The values
of these key fields are passed to the data source control in order to
specify the row to update or delete.
Note: it's been a while since I used this, so you might need to include both the primary key, and your other key field. Like this:
DataKeyNames="id,regGroupID"
I am trying to make a dropdownlist work for me. A user can be in many 'Field1's, so I wanted the dropdown to show those Field1s.
Currently what happens is I just get a blank list. Here is a sample of the code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT DISTINCT Field1 FROM table
WHERE (Field3= #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
If I remove the select parameters and the WHERE clause the list of all DHBs appears - it just seems to have issues with #Field3.
Edit: I have tested the query in SQL Server and it works for me (by replacing #Field3 with a known value)
Any suggestions? I am very new to asp.net, and have next to nothing in the .cs file (besides some authorisation and such), so if people could point me in the right direction that would be fantastic.
Edit 2: It would probably help if I gave you the whole template:
<asp:TemplateField HeaderText="Field1" SortExpression="Field2">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Field2") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="Field1">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="Field1 FROM table
WHERE (Field3 = #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
You're populating another control using Dropdownlist selectedValue as a parameter, that is dictated by your code. In this case
You're missing property in the controlParameter
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Property="SelectedValue" Name="Field3" Type="String" />
</SelectParameters>
If you're trying to populate DropDownList1 you're completely derailed. In this case you need to show up more markup code and details.
Update: You cannot take the parameters value from the DropDownList control which you're populating. Either remove the parameter or use another control to provide the value
I have a SqlDataSource that calls a stored procedure and it works fine. If I add a <ControlParameter> tag to add an additional argument, then the query never fires and the databinding never occurs. Suggestions?
This works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultDB %>"
SelectCommand="SP_WHATEVER" SelectCommandType="StoredProcedure"
UpdateCommand="SP_WHATEVER2" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="UserName" SessionField="RP_Program" Type="String" />
</SelectParameters>
<UpdateParameters>
<snip...>
</UpdateParameters>
</asp:SqlDataSource>
When I add the ControlParameter, the databinding no longer occurs:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultDB %>"
SelectCommand="SP_WHATEVER" SelectCommandType="StoredProcedure"
UpdateCommand="SP_WHATEVER2" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="UserName" SessionField="RP_Program" Type="String" />
<asp:ControlParameter Name="SprocArgName" ControlID="ddlFilter" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<snip...>
</UpdateParameters>
</asp:SqlDataSource>
The ControlParameter refers to a valid object on the page. Any other suggestions?
Most likely one of the parameter is empty or null. Add CancelSelectOnNullParameter="false" to the asp:SqlDataSource and ConvertEmptyStringToNull="true" to both parameters. Once it works, tweak the parameters so that SP gets what it expects.