I haveasp:GridView displaying client requests using asp:SqlDataSource. I want to limit displayed information by client:
View.aspx has to display everything, View.aspx?client=1 has to display only requests from client ID #1.
So I'm using <asp:QueryStringParameter Name="client" QueryStringField="client" /> for query "EXEC getRequests #client".
Everything works properly when some client is specified. But don't - if not.
I tested my SP using SSMS - it works properly in both cases - when parameter is specified and when it isn't (NULL passed explicitly).
What have I do?
SqlDataSource won't fire if any of it's parameters are null, unless you specify otherwise:
<asp:SqlDataSource CancelSelectOnNullParameter="False" />
It might also be necessary to add a null default value to your querystring parameter:
<asp:QueryStringParameter Name="client" QueryStringField="client" DefaultValue="" ConvertEmptyStringToNull="True" />
You need to define a Default value to the parameter for those situations, for example:
<asp:QueryStringParameter Name="client" QueryStringField="client" DefaultValue="0"/>
and then in the SP you need verify if the client is 0, return all the clients, otherwise the specific one.
Related
I am using ASP Classic 2.0 so I know some of this code is a bit outdated but it is what I inherited. I am trying to use a variable from my backing aspx.vb page in the select query.
I have the following query (truncated)
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="Data Source=.;Initial Catalog=dataSQL;Persist Security Info=True;User ID=user;Password=$$$$"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT ClID, Client_Name
FROM [tblClient]
WHERE (tblClient.ClID = #ClID )"
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ClID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
I want to replace the ControlParameter with a variable from the aspx.vb that backs this aspx page.
I know I have access to the variable because <%=ClID%> produces the correct ID but adding something .Add("ClID", DbType.Guid, clID) does not work.
I am guessing there is an easy way to do this.
If it needs to be a control I think I can bind ClID to the a form control but I cannot figure out the correct syntax for that.
Thanks,
Steve
First, for the <%=CLID%> remark remember that <%= is just shorthand for Response.Write(), meaning the result is only written into the final html rendered to the browser. It does not and never will insert values into aspx markup.
The .Add() code, however, is on the right track, but we still need to make two adjustments.
First, in the aspx markup use an <asp:Parameter instead of <asp:ControlParameter.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="Data Source=.;Initial Catalog=dataSQL;Persist Security Info=True;User ID=user;Password=$$$$"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT ClID, Client_Name
FROM [tblClient]
WHERE tblClient.ClID = #ClID"
<SelectParameters>
<asp:Parameter Name="ClID" DbType="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Then, in the aspx.vb code-behind instead of .Add() you must remember the parameter already exists. If you try to add it again you'll get an exception. Instead, you need to set the Value for the existing parameter in your code-behind:
SqlDataSource1.SelectCommand.Parameters["ClID"].Value = ClID
It's been a loooong time since I've used code like this, so you may also need to call .DataBind()
I am using asp.net sqldatasource connected to formview which has update parameter defined as string like such:
<UpdateParameters>
<asp:Parameter Name="C" Type="String" />
</UpdateParameters>
When I exceed 4000 characters I get "String truncation: max=4000..." error.
I am updating to SQL Compact Edition 4.0 and field is ntext.
I was able to put more than 4000 characters usign visual studio interface the problem seems not to be with the database field type or size.
Changing the parameter to:
<asp:Parameter Name="C" Type="String" size="5000" />
did not help. If I do this I get no errors but no more than 4000 characters get updated.
What could be limiting the update character size?
I ended up moving database update at code behind
Using connection As New SqlCeConnection(connstr)
Dim nonquerycommand As SqlCeCommand = connection.CreateCommand()
nonquerycommand.Parameters.Add("#c", SqlDbType.NText).Value = TheData
connection.Open()
nonquerycommand.CommandText = queryString
nonquerycommand.ExecuteNonQuery()
connection.Dispose()
End Using
I still wonder why I hit the 4000 character limit when I use SqlDatasource with asp:Parameter tag. Looking at the code, adding SqlDbType.NText parameter may be the issue. I could not add this on aspx page to the asp:Parameter tag.
I wish I could handle this on aspx page with SqlDataSource Update Parameters. But it wont work for updating more than 4000 Characters.
I have to make revisions on a number of projects so If there is an anwer to my initial question it will save me time. Although codebehind solution works for more than 4000 characters, I won't have to change for each project if sqldatasource in aspx page handles 4000+ characters - so help is appreciated.
This is driving me crazy, and even though it doesn't really matter right now I'd like to know what's going on.
I have a Stored Procedure called ClientSelect:
SELECT * FROM dbo.Client
That's all that's in it (I've stripped it right back to try and figure out what's going on!)
I also have an ASP.NET page with a GridView. Its DataSource looks like this:
<asp:SqlDataSource ID="SQLClient" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="EXEC ClientSelect" >
<SelectParameters>
<asp:Parameter Name="ClientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Note that the SelectCommand does not make any reference to the SelectParameter, and the Stored Procedure does not ask for any parameters. This page loads, but the GridView is empty - no data returned.
However, if I remove the SelectParameter completely, or add a default value so that it looks like:
<asp:Parameter Name="ClientID" Type="Int32" DefaultValue="1" />
It returns all the records in the table (not filtering by the specified default value).
Why is the explicit coding of a SelectParameter causing no data to be returned, even though neither my SelectCommand or my Stored Procedure are making any reference to it at all? It doesn't make sense to me!
can you please try this ??
<asp:SqlDataSource ID="SQLClient" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="ClientSelect" SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:Parameter Name="ClientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Well, I'm not a specialist about ASP.NET, but I see, that if your SP doesn't have any parameters, but still you specify them on the query - it should throw an exception, unless you've turned this kind of exceptions (SQL SERVER) off.
Besides, if there is a parameter specified, but no value applied, it also causes an error, because normally any code is transformed by the driver you access the SQL Server into the T-SQL code. So in the first time you will get this:
EXEC ClientSelect #ClietntId=
And next one is that
EXEC ClientSelect #ClietntId=1
which is ok.
Well, people say if there are parameters specified, that are not exist, they are ignored. DefaultValue should be. Otherwise its better to try code #Dhaval suggested and give a feedback.
I think you need to specify a way to get the actual value for the defined parameter.
For example; if you are getting parameter value from a session variable you have to define your SelectParameter as
<!-- To get ClientID from a session variable called myClientId -->
<asp:SessionParameter Name="ClientID" DbType="Int16"
DefaultValue="0" SessionField="myClientId"/>
<!-- To Get ClientID from a dropdown List called ddlClients -->
<asp:ControlParameter Name="ClientID" DbType="Int16"
DefaultValue="0" ControlID="ddlClients"/>
<!-- YOUR CODE -->
<asp:SqlDataSource ID="SQLClient" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="ClientSelect" SelectCommandType="StoredProcedure" >
<SelectParameters>
<!-- Add correct parameter type as appropriate -->
</SelectParameters>
</asp:SqlDataSource>
The following link passes query parameters as f:param and they get substituted in Users.page.xml, and these parameters appear as query parameters in the browse URL which we would like to not show to the end user. Is there an alternate mechanism to pass parameters to the entity query bean
<rich:menuItem>
<s:link value="Users" view="/Users.xhtml">
<f:param name="gender" value="male" />
</s:link>
</rich:menuItem>
With s:link you are doing a GET request, the parameter will be in the URL. To hide the parameter you can change this to a h:commandButton calling an action with the parameter:
<h:commandButton action="#{bean.listUsers('male')}" >
Very simple question but all the answer I read over the web doesn't apply.
I try to do an update on a ASP.NET Gridview but when I click on update, I get this error:
Incorrect Syntax near 'nvarchar'. The scalar variable #intID must be declare.
Here is my datasource. I guess the problem come from here but I can't see where...
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connexionDev %>"
DeleteCommand="DELETE FROM [tbl_Bug] WHERE intID = #intID"
SelectCommand="SELECT [intID],[strTitre],[strDescription],[intStatus_FK],[intType_FK],[intSeriousness_FK] FROM [tbl_Bug]"
UpdateCommand="UPDATE [tbl_Bug] SET [strTitre] = #strTitre ,[strDescription] = #strDescription ,[intStatus_FK] = #intStatus_FK ,[intType_FK] = #intType_FK ,[intSeriousness_FK] = #intSeriousness_FK WHERE [intID] = #intID">
<DeleteParameters>
<asp:Parameter Name="intID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="intID" Type="Int32" />
<asp:Parameter Name="strTitre" Type="String" />
<asp:Parameter Name="strDescription" Type="String" />
<asp:Parameter Name="intStatus_FK" Type="Int32" />
<asp:Parameter Name="intType_FK" Type="Int32" />
<asp:Parameter Name="intSeriousness_FK" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Thanks for your help in advance!
EDIT - EDIT - EDIT
Well, I wanted to use SQL Profiler but it seems that it's not in my version (SQL server 2008 Express) so I tried another sql profiler that is open source but I never understood how it worked and it was always crashing ...
Is there any other way to know the query that are used so I can track my problem?
Hi everyone and thanks for your help
My query was good, it was the binding in the GridView that was bad, for a very very simple detail
This will work: Text='<%# Bind("myValue") %>'
While this wont: Text='<%# Bind("[myValue]") %>'
So watch out! :) Now everything is working!
Thanks again!
In cases like this the answer is often to run SQL Profiler and see what SQL is being sent, it's often a mismatch in variable names or something equally simple once you see what being sent to your SQL Server.
You can register an event handler for SqlDataSource.Updating event - it will be raised when the command is prepared, and before executing it. The command will be a DbCommand object, which can be retrieved from an instance of SqlDataSourceCommandEventArgs that your event handler will get via its Command property. Dump both the text, and the actual names and values of all command parameters (in DbCommand.Parameters collection), and check it for errors.
I think you need <SelectParameters>...</SelectParameters> section where you declare all your fields.
This is always the case when you have spaces in your field/column names - rename the DB columns and remove spaces - it will work .
If you look at the SQL server monitor and inspect the generated SQL code for Update - it is messed up due to spaces in column names.