Showing posts with label parameters. Show all posts
Showing posts with label parameters. Show all posts

Friday, March 23, 2012

Of Multiple Parameters, SqlDataSource and Text boxes

Hi,
Hope if someone can help me here. Keep in mind I an fairly new to .NET and SQL and am learning to break my MS Access habit :)


I have a web form that is using a SqlDataSource and a FormView control. In addition to this I have 2 text boxes. What I am trying to do is display results in the FormView based on what a user types into one of the Text Boxes (one or the other…Not both)

The SELECT statement in the SqlDataSource looks like this in concept.

SELECT Field1, Field2, Field3, Field4
FROM dbo.MYTABLE
WHERE (Field1 = @.Field1) AND (Field2 IS NULL)
OR (Field2 = @.Field2) AND (Field1 IS NULL)

I have the two text boxes pointing at the parameters (@.Field1 and @.Field2) so in theory I would expect that when a user populates one of the text boxes and clicks a button to databind the FormView it would display a record matching that criteria…. But it's not all I get is a blank/missing FormView.

I tried different variations on the SQL statement and tried using = '' instead of IS NULL but still the same results.
However, if I populate one text box with a value that I know is not in my table and populate the other with a value of which I know exists in my table is…It works.
What am I missing?


SELECT Field1, Field2, Field3, Field4
FROM dbo.MYTABLE
WHERE (Field1 = @.Field1) AND (Field2 IS NULL)
OR (Field2 = @.Field2) AND (Field1 IS NULL)

Should be:

SELECT Field1, Field2, Field3, Field4
FROM dbo.MYTABLE
WHERE (Field1 = @.Field1) AND (@.Field2 IS NULL)
OR (Field2 = @.Field2) AND (@.Field1 IS NULL)

However, I suspect that there are other issues you are having, as your original statement would have only worked if you actually had a NULL in one of your search fields. Please copy and paste the SqlDataSource control from the .ASPX page, as I'm guessing that it's not really a logical problem, more like an oops, I knew that problem.

|||

Opps yeah...That was a typo on my part. Well the field in the text box is blank and I have "ConvertEmptyString ToNull" in the parameters advanced properties set to true. But, your right it is behaving as though the query not seeing null.

Is there syntax to set the default value of the parameter to null in the Configure Data Source/Query Builder pop up ? (Using VS 2005)

|||

Here is the code:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:QualityConnectionString %>"

SelectCommand="SELECT WWID, WONumber, WorkWeek, ID FROM dbo.WorkOrderInfo WHERE (ID = @.ID) AND (@.WONumber = '') OR (@.ID = '') AND (WONumber = @.WONumber)" EnableCaching="True">

<FilterParameters>

<asp:ControlParameter ControlID="TextBox1" Name="ID" PropertyName="Text" />

</FilterParameters>

<SelectParameters>

<asp:ControlParameter ControlID="TextBox1" Name="ID" PropertyName="Text" />

<asp:ControlParameter ControlID="TextBox2" Name="WONumber" PropertyName="Text" />

</SelectParameters>

</asp:SqlDataSource>

|||

Remove the filter parameters section, it isn't needed, and would cause some queries to fail when they shouldn't.

Also, you either need to change ConvertEmptyStringsToNull (Real property name should be close) to false, OR change the ='' parts of your query to IS NULL, since the empty string parameters are being converted to NULL. That should take care of it for you.

|||

OK have this now and still not working. Could it have something to do with VB passing NULL as Nothing?

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:QualityConnectionString %>"

SelectCommand="SELECT WONumber, ID, WWID, WorkWeek FROM dbo.WorkOrderInfo WHERE (ID = @.ID) AND (WONumber IS NULL) OR (ID IS NULL) AND (WONumber = @.WONumber)">

<SelectParameters>

<asp:ControlParameter ControlID="TextBox1" Name="ID"

PropertyName="Text" Type="Int32" DefaultValue="" />

<asp:ControlParameter ControlID="TextBox2" Name="WONumber" PropertyName="Text"

Type="Int32" />

</SelectParameters>

</asp:SqlDataSource>

|||

took out the default values...same result

OK have this now and still not working. Could it have something to do with VB passing NULL as Nothing?

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:QualityConnectionString %>"

SelectCommand="SELECT WONumber, ID, WWID, WorkWeek FROM dbo.WorkOrderInfo WHERE (ID = @.ID) AND (WONumber IS NULL) OR (ID IS NULL) AND (WONumber = @.WONumber)">

<SelectParameters>

<asp:ControlParameter ControlID="TextBox1" Name="ID"

PropertyName="Text" Type="Int32" />

<asp:ControlParameter ControlID="TextBox2" Name="WONumber" PropertyName="Text"

Type="Int32" />

</SelectParameters>

</asp:SqlDataSource>

|||

I'm pretty sure you have it right. Try making a new page, then copy/paste the SqlDataSource on it. Drag 2 NEW textboxes on the new page, and drag a NEW gridview on the page. Configure the grid to display data from the sqldatasource, and run the page and see if it works.

|||Tried from scratch same result. The empty text boxes must not be passing a Null. I found a work around by setting the default paramater values to a negative integer and leaving out the @.parameter IS NULL ...But this is driving me insane!|||

try this instead:

SELECT WONumber, ID, WWID, WorkWeek FROM dbo.WorkOrderInfo WHERE ((ID = @.ID) OR (@.ID IS NULL)) AND ((WONumber = @.WONumber) OR (@.WONumber IS NULL))

The parameters should have no defaults, and convertemtystringtonull should be true.

|||

That didn't work either. Setting ConvertEmptyString ToNull toFalse and the below SQL statement works though. I thought I tried that before but must have missed a parameter property setting.

SELECT WONumber, ID, WWID, WorkWeek FROM dbo.WorkOrderInfo WHERE (ID = @.ID) OR (WONumber = @.WONumber)

This is odd because I have another page with a reportviewer that uses a table adaptor etc. and several text boxes for passing parameters to a stored procedure to generate data for the report. Any one of them can be null and it works great. Not sure what is going on here though.

|||I also changed to parameter type to "Empty" (default) not int32...|||

Agh!! I was wrong it actualy does not work the way I wanted. Seems the empty string (blank text box) for the WONumber parameter thinks it is a zero too. So for any WONumber that is 0 and the text box being empty, it will retireve that record.

Theonlyway I can get to work is a stored procedure.

CREATE PROCEDURE dbo.SelectWO(@.ID int,
@.WONumber int )
AS
If @.ID = ''
SET @.ID = Null
If @.WONumber = ''
SET @.WONumber = Null
SELECT ID, WWID, WONumber,FROM dbo.WorkOrderInfo
WHERE (ID = @.ID AND @.WONumber IS NULL) OR (WONumber = @.WONumber AND @.ID IS NULL)

OR( @.ID IS NULL AND @.WONumber IS NULL)

ORDER BY ID
GO

Had to leave ConvertEmptyStringtoNull to false and convert in my SP. setting it to True in VS breaks the SP.

Anyone ...try to build a web form with 2 search parameters getting thier values from to text boxes see if you can get it working cause I could not.


|||

/sigh

CREATE PROCEDURE dbo.SelectWO(@.ID int,
@.WONumber int )
AS
SELECT ID, WWID, WONumber,FROM dbo.WorkOrderInfo
WHERE (ID = @.ID AND @.WONumber IS NULL) OR (WONumber = @.WONumber AND @.ID IS NULL)OR( @.ID IS NULL AND @.WONumber IS NULL)
ORDER BY ID
GO

Is the SP you want. Change the ConvertEmptyStringToNull's on both parameters to true, and explicitly set the parameter types to "int"/"integer". Also make sure you set the sqldatasouce property "CancelOnNullParameter" to false.

It'll work.

The problem is that yes, if you leave ConvertEmptyStringToNull false, the paramters will be converted to 0. Your checks IF @.ID='' will never be true, but @.ID is an int, and int's can never be an empty string. They can only be an integer or NULL.

Wednesday, March 21, 2012

Odd sqldatasource insert behavior

I have a sqldatasource (code listed below) whose insert Paramaters are control parameters. My aspx page has a textbox and a submit button. the button onclick runs the sqdatasource1.insert.

What I get is every other insert inserts the text in textbox2 and every other insert enters nothing for the namecust value. I have a required field validator which correctly prevents submission if textbox2 is empty.

How do I fix this?

:<code>

<asp:PanelID="Panel1"runat="server"Height="50px"Width="548px">

<asp:ButtonID="Button1"runat="server"Text="New Prospect"ValidationGroup="insertCust"/>

<asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ControlToValidate="TextBox2"

ErrorMessage="Prospect Name can not be blank"ValidationGroup="insertCust"></asp:RequiredFieldValidator>

<asp:TextBoxID="TextBox2"runat="server"Width="330px"ValidationGroup="insertCust"></asp:TextBox></asp:Panel>

<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:AccPac2ConnectionString %>"

SelectCommand="SELECT DISTINCT CODETERR FROM dbo.F_arcus() AS F_arcus_1 WHERE (DATEINAC = 0) AND (rtrim(CODETERR) <>'')">

</asp:SqlDataSource>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:AccPac2ConnectionString %>"

InsertCommand="INSERT INTO dbo.BudgetProspects(NameCust, CodeTerr) VALUES (@.Namecust, @.codeterr)"

SelectCommand="SELECT CustomerID, NameCust FROM dbo.BudgetProspects WHERE (CodeTerr = @.codeterr)"

UpdateCommand="UPDATE dbo.BudgetProspects SET NameCust = @.namecust">

<UpdateParameters>

<asp:ParameterName="namecust"/>

</UpdateParameters>

<SelectParameters>

<asp:ControlParameterControlID="RadioButtonList1"Name="codeterr"PropertyName="SelectedValue"/>

</SelectParameters>

<InsertParameters>

<asp:ControlParameterControlID="textbox2"Name="Namecust"PropertyName="text"/>

<asp:ControlParameterControlID="RadioButtonList1"Name="codeterr"PropertyName="SelectedValue"/>

</InsertParameters>

</asp:SqlDataSource>

</code>

codebehind button_click:

<code>

ProtectedSub Button1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.Click

IfNot TextBox2.TextIsNothingThen

SqlDataSource1.Insert()

TextBox2.Text =""

EndIf

EndSub

</code>

Never mind, complete stupidity on my part the altenate row style had white background and white text. Embarrassed [:$]

Tuesday, March 20, 2012

Odd error with Reporting Services and Oracle

I've got a fairly simple report that hits oracle with two parameters set as strings.

When I run the query in the designer, I get

Error Source: System.Data.OracleClient
Error Message: ORA-01858: a non-numeric character was found where a numeric was expected

However, when I run the report in preview everything just works. Same inputs on the parameters.

Any ideas?

What types are your parameters and what is the SQL you are using in your DataSet? I've seen this kind of error passing a parameter that is declared as a string in the report to an oracle parameter that is compared against a Date type in Oracle.

Mike

|||Sorery I meant to post a followup to this. The solution is that SQL RS 2k5 takes a string that happens to be a date and makes it a date without telling.

When it goes into oracle as this "Date" it will go in in the default SQL date format. So you need to match the format mask in the oracle TO_Date function call to the 3 letter month sql format.

Interesting, RS 2K did not do this.

Saturday, February 25, 2012

ODBC SPROCS and unnamed parameters

I have a stored procedure outlined below that I access on SQL Server
6.5 via ODBC.
CREATE PROCEDURE usp_SQLREPORTING_TEST @.userid VARCHAR(6) AS
SELECT * INTO #TEMP_CM FROM tblUser WHERE userID LIKE @.userid
SELECT * FROM #TEMP_CM
When I add the data set to SQL reporting services I cannot opt for
command type "Stored procedure" on an ODBC connection , thus I choose
text and execute the dataset with thye following command using a
unnamed parameter.
exec usp_SQLREPORTING_TEST ?
Data is correctly returned when i run the query, BUT I receive no
fields in the data set thus the report never shows anything. A "build"
reveals an error message stating that "the filed in missing from the
returned resultset"
Help!!!!Try clicking the "Refresh Fields" button in Data tab in designer.
--
Ravi Mumulla (Microsoft)
SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Ken Shabby" <paul.scott@.klm.com> wrote in message
news:872f3c38.0410060425.3de9c4ce@.posting.google.com...
> I have a stored procedure outlined below that I access on SQL Server
> 6.5 via ODBC.
>
> CREATE PROCEDURE usp_SQLREPORTING_TEST @.userid VARCHAR(6) AS
> SELECT * INTO #TEMP_CM FROM tblUser WHERE userID LIKE @.userid
> SELECT * FROM #TEMP_CM
>
> When I add the data set to SQL reporting services I cannot opt for
> command type "Stored procedure" on an ODBC connection , thus I choose
> text and execute the dataset with thye following command using a
> unnamed parameter.
> exec usp_SQLREPORTING_TEST ?
> Data is correctly returned when i run the query, BUT I receive no
> fields in the data set thus the report never shows anything. A "build"
> reveals an error message stating that "the filed in missing from the
> returned resultset"
> Help!!!!

odbc select where parameters

Im enabling an apllication to use ODBC to connect to sqlserver which currently uses Oracle OCI, i have no prior knowledge about odbc use.
Im unsure how to approach where clause parameters (bind parameters)
ie Oracle OCI
select name into :name from emp where name = :a_name

via ODBC, connecting to sql server i'm attempting

select name from emp where name = ?

with,
sqlprepare
sqlbindparameter
sqlexecute
sqlbindcol
sqlfetch

all seems ok with sqlbindparameter, but sqlexecute fails with sqlstate 22001, String data, right truncation.
The question: can i use ? parameter in where conditions, if not whats the best approach.
Many Thanks.resolved. problem was oci doesnt require null terminated char string, odbc does

ODBC named parameters ??

Today we tried to connect to an Informix server and make a report, worked
fine until I need to put in parameters, then it said that the odbc
connection couldn't use named parameters and should use unnamed
parameters.
I also tried using an ole db connection with the same result.
What should I do to make use of parameters '
Jack
---
Jeg beskyttes af den gratis SPAMfighter til privatbrugere.
Den har indtil videre sparet mig for at få 46338 spam-mails.
Betalende brugere får ikke denne besked i deres e-mails.
Hent gratis SPAMfighter her: www.spamfighter.dkUse unnamed (put a ? in). Note that it will name your report parameters
unfriendly names. I always rename them then go back to the dataset, click on
..., parameters tab and remap the query parameters to the report parameters.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Jack Nielsen" <no_spam jack.nielsen@.get2net.dk> wrote in message
news:uiNFyOy3FHA.3136@.TK2MSFTNGP09.phx.gbl...
> Today we tried to connect to an Informix server and make a report, worked
> fine until I need to put in parameters, then it said that the odbc
> connection couldn't use named parameters and should use unnamed
> parameters.
> I also tried using an ole db connection with the same result.
> What should I do to make use of parameters '
> Jack
>
> ---
> Jeg beskyttes af den gratis SPAMfighter til privatbrugere.
> Den har indtil videre sparet mig for at få 46338 spam-mails.
> Betalende brugere får ikke denne besked i deres e-mails.
> Hent gratis SPAMfighter her: www.spamfighter.dk
>|||Hi Jack,
The .NET managed providers for ODBC and OleDb do not support named parameters.
You'll need to use ?'s to mark where the parameters ought to appear in your
SQL statement, and then make sure you add them in the proper order that they
appear in the SQL.
Best,
-Chris
> Today we tried to connect to an Informix server and make a report,
> worked fine until I need to put in parameters, then it said that the
> odbc connection couldn't use named parameters and should use unnamed
> parameters.
> I also tried using an ole db connection with the same result.
> What should I do to make use of parameters '
> Jack
> ---
> Jeg beskyttes af den gratis SPAMfighter til privatbrugere.
> Den har indtil videre sparet mig for at få 46338 spam-mails.
> Betalende brugere får ikke denne besked i deres e-mails.
> Hent gratis SPAMfighter her: www.spamfighter.dk|||> The .NET managed providers for ODBC and OleDb do not support named
> parameters. You'll need to use ?'s to mark where the parameters ought to
> appear in your SQL statement, and then make sure you add them in the
> proper order that they appear in the SQL.
Do you mean that the order of the report parameters have to be the order
in
which the ? appears in the sql statement ?
I'm a bit confused here :)
Jack
---
Jeg beskyttes af den gratis SPAMfighter til privatbrugere.
Den har indtil videre sparet mig for at få 46339 spam-mails.
Betalende brugere får ikke denne besked i deres e-mails.
Hent gratis SPAMfighter her: www.spamfighter.dk|||No, the order of the mapping. Important concept. Even though RS makes it
seem like they are one and the same they aren't. There are Query Parameters
and Report Parameters. The query parameters are mapped to the report
parameters (click on the ..., parameters tab to see where you change this).
Note that query parameters can be mapped to an expression, they don't have
to be mapped to a report parameter. RS automatically creates report
parameters.
The order is the order of the mapping (dataset tab). In the layout tab,
report parameters, they can be in any order.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Jack Nielsen" <no_spam jack.nielsen@.get2net.dk> wrote in message
news:u%23xGgEz3FHA.1188@.TK2MSFTNGP12.phx.gbl...
>> The .NET managed providers for ODBC and OleDb do not support named
>> parameters. You'll need to use ?'s to mark where the parameters ought to
>> appear in your SQL statement, and then make sure you add them in the
>> proper order that they appear in the SQL.
> Do you mean that the order of the report parameters have to be the order
> in
> which the ? appears in the sql statement ?
> I'm a bit confused here :)
> Jack
>
> ---
> Jeg beskyttes af den gratis SPAMfighter til privatbrugere.
> Den har indtil videre sparet mig for at få 46339 spam-mails.
> Betalende brugere får ikke denne besked i deres e-mails.
> Hent gratis SPAMfighter her: www.spamfighter.dk
>|||> No, the order of the mapping. Important concept. Even though RS makes it
> seem like they are one and the same they aren't. There are Query
> Parameters and Report Parameters. The query parameters are mapped to the
> report parameters (click on the ..., parameters tab to see where you
> change this). Note that query parameters can be mapped to an expression,
> they don't have to be mapped to a report parameter. RS automatically
> creates report parameters.
> The order is the order of the mapping (dataset tab). In the layout tab,
> report parameters, they can be in any order.
I've come a bit further but it now tells me something about setparameterinfo
when i try to run the statement in the data tab.
Jack|||Are you doing a query or calling a stored procedure?
I always use the generic query design window, the button to switch to it is
to the right of the ...
Are you using OLEDB or ODBC?
I go against Sybase and did better with ODBC than OLEDB.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Jack" <jackdSPAM@.jackd.dk> wrote in message
news:uyb%23c%2373FHA.3592@.TK2MSFTNGP12.phx.gbl...
>> No, the order of the mapping. Important concept. Even though RS makes it
>> seem like they are one and the same they aren't. There are Query
>> Parameters and Report Parameters. The query parameters are mapped to the
>> report parameters (click on the ..., parameters tab to see where you
>> change this). Note that query parameters can be mapped to an expression,
>> they don't have to be mapped to a report parameter. RS automatically
>> creates report parameters.
>> The order is the order of the mapping (dataset tab). In the layout tab,
>> report parameters, they can be in any order.
> I've come a bit further but it now tells me something about
> setparameterinfo when i try to run the statement in the data tab.
> Jack
>|||> Are you doing a query or calling a stored procedure?
> I always use the generic query design window, the button to switch to it
> is to the right of the ...
> Are you using OLEDB or ODBC?
> I go against Sybase and did better with ODBC than OLEDB.
I've tried using oledb and odbc, now i'm using odbc against an informix
server. It's a normal query using the sql designer, haven't tried the
generic.
Jack|||Try having a single parameter. Hard code the rest. Execute from the data tab
and you should be prompted to put in the value. If this doesn't work then
you could be having a problem with the ODBC driver. Note that when putting
in the parameter value if it is a string or a date just put the value in.
For instance for a data just put in 9/1/2005 for example.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Jack" <jackdSPAM@.jackd.dk> wrote in message
news:OCKRHUF4FHA.3400@.tk2msftngp13.phx.gbl...
>> Are you doing a query or calling a stored procedure?
>> I always use the generic query design window, the button to switch to it
>> is to the right of the ...
>> Are you using OLEDB or ODBC?
>> I go against Sybase and did better with ODBC than OLEDB.
> I've tried using oledb and odbc, now i'm using odbc against an informix
> server. It's a normal query using the sql designer, haven't tried the
> generic.
> Jack
>