469,602 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Select string building in C# and ASP.NET

How would I go about using a custom select string that is passed from a form
to the SelectCommand parameter of SqlDataSource?

I tried:
SelectCommand = "<% Request.Form("hdnSelect") %>"

but I got an error about putting <% %> tags in a literal. I tried taking
out the quotes (the " ") and it didn't do any good. I'm trying to use the
built in ASP.NET 2.0 DataList control, but I haven't come up with anything.

Thanks,
Jacob
Dec 22 '05 #1
8 1820
Hi,

Did you tried:

SelectCommand = Request.Form("hdnSelect").ToString()

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jacob Arthur" <ja******@community.nospam> wrote in message
news:e$**************@TK2MSFTNGP09.phx.gbl...
How would I go about using a custom select string that is passed from a
form to the SelectCommand parameter of SqlDataSource?

I tried:
SelectCommand = "<% Request.Form("hdnSelect") %>"

but I got an error about putting <% %> tags in a literal. I tried taking
out the quotes (the " ") and it didn't do any good. I'm trying to use the
built in ASP.NET 2.0 DataList control, but I haven't come up with
anything.

Thanks,
Jacob

Dec 22 '05 #2
Hi Jacob,

For dynamically setting the DataSource control's select command, we can use
code behind to programmatically assign certain values to it as Ignacio has
mentioned. e.g:
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "Select top 1 * from Categories";
}
Also, if what you want to customize is just some certain select
criterias(parameters), we can also consider using the SqlDataSource's
selectParameters to deine tha variable and associate those parameter
variables to request.Form colleciton. e.g:

=================================
CategoryID > <input type="text" name="cid" value="0" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" /><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalNorthWind %>"
SelectCommand="SELECT [Description], [CategoryName],
[CategoryID] FROM [Categories] WHERE ([CategoryID] > @CategoryID)">
<SelectParameters>
<asp:FormParameter FormField="cid" Name="CategoryID"
Type="Int32" DefaultValue="4" />
</SelectParameters>
</asp:SqlDataSource>

=============================

#FormParameter Class
http://msdn2.microsoft.com/en-us/lib...rols.formparam
eter.aspx
Hope also helps.

Thanks & Merry Christmas!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>
| References: <e$**************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: Select string building in C# and ASP.NET
| Date: Thu, 22 Dec 2005 13:26:13 -0500
| Lines: 32
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <uS*************@TK2MSFTNGP12.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.framework.aspnet,microsoft .public.dotnet.languages.c
sharp
| NNTP-Posting-Host: 156.75.83.95
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:373711
microsoft.public.dotnet.framework.aspnet:366621
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| Did you tried:
|
| SelectCommand = Request.Form("hdnSelect").ToString()
|
|
|
| --
| Ignacio Machin,
| ignacio.machin AT dot.state.fl.us
| Florida Department Of Transportation
|
|
| "Jacob Arthur" <ja******@community.nospam> wrote in message
| news:e$**************@TK2MSFTNGP09.phx.gbl...
| > How would I go about using a custom select string that is passed from a
| > form to the SelectCommand parameter of SqlDataSource?
| >
| > I tried:
| > SelectCommand = "<% Request.Form("hdnSelect") %>"
| >
| > but I got an error about putting <% %> tags in a literal. I tried
taking
| > out the quotes (the " ") and it didn't do any good. I'm trying to use
the
| > built in ASP.NET 2.0 DataList control, but I haven't come up with
| > anything.
| >
| > Thanks,
| > Jacob
| >
|
|
|

Dec 23 '05 #3
Just tried something a little different, still no luck.

I tried putting the line
SqlDataSource1.SelectCommand = docList;

In to the Page_Load event to make sure it was getting there and it did
during debugging, but there is no data being pulled back. I traded out the
command for

SqlDataSource1.SelectCommand = "select '1'";

and it still doesn't bring anything back in the datalist. Am I missing
something here possibly?

Jacob

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uS*************@TK2MSFTNGP12.phx.gbl...
Hi,

Did you tried:

SelectCommand = Request.Form("hdnSelect").ToString()

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jacob Arthur" <ja******@community.nospam> wrote in message
news:e$**************@TK2MSFTNGP09.phx.gbl...
How would I go about using a custom select string that is passed from a
form to the SelectCommand parameter of SqlDataSource?

I tried:
SelectCommand = "<% Request.Form("hdnSelect") %>"

but I got an error about putting <% %> tags in a literal. I tried taking
out the quotes (the " ") and it didn't do any good. I'm trying to use
the built in ASP.NET 2.0 DataList control, but I haven't come up with
anything.

Thanks,
Jacob


Dec 23 '05 #4
Hi Jacob,

Have you also had a look in my last reply? Also, as for
SqlDataSource.SelectCommand , it should be assigned a valid select
statement which will return record set from database, and those records
should contains valid Properties that mapping to the Fields or Columns we
defined in dataBound control (GridView or DetailsView. .....).

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| Reply-To: "Jacob Arthur" <jacob-no@--spam-atstn.com>
| From: "Jacob Arthur" <ja******@community.nospam>
| References: <e$**************@TK2MSFTNGP09.phx.gbl>
<uS*************@TK2MSFTNGP12.phx.gbl>
| Subject: Re: Select string building in C# and ASP.NET
| Date: Thu, 22 Dec 2005 21:56:19 -0600
| Lines: 52
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <uQ**************@tk2msftngp13.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.framework.aspnet,microsoft .public.dotnet.languages.c
sharp
| NNTP-Posting-Host: h460ad645.area7.spcsdns.net 70.10.214.69
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:373796
microsoft.public.dotnet.framework.aspnet:366713
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Just tried something a little different, still no luck.
|
| I tried putting the line
| SqlDataSource1.SelectCommand = docList;
|
| In to the Page_Load event to make sure it was getting there and it did
| during debugging, but there is no data being pulled back. I traded out
the
| command for
|
| SqlDataSource1.SelectCommand = "select '1'";
|
| and it still doesn't bring anything back in the datalist. Am I missing
| something here possibly?
|
| Jacob
|
| "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
| in message news:uS*************@TK2MSFTNGP12.phx.gbl...
| > Hi,
| >
| > Did you tried:
| >
| > SelectCommand = Request.Form("hdnSelect").ToString()
| >
| >
| >
| > --
| > Ignacio Machin,
| > ignacio.machin AT dot.state.fl.us
| > Florida Department Of Transportation
| >
| >
| > "Jacob Arthur" <ja******@community.nospam> wrote in message
| > news:e$**************@TK2MSFTNGP09.phx.gbl...
| >> How would I go about using a custom select string that is passed from
a
| >> form to the SelectCommand parameter of SqlDataSource?
| >>
| >> I tried:
| >> SelectCommand = "<% Request.Form("hdnSelect") %>"
| >>
| >> but I got an error about putting <% %> tags in a literal. I tried
taking
| >> out the quotes (the " ") and it didn't do any good. I'm trying to use
| >> the built in ASP.NET 2.0 DataList control, but I haven't come up with
| >> anything.
| >>
| >> Thanks,
| >> Jacob
| >>
| >
| >
|
|
|

Dec 23 '05 #5
Hi,

is your problem in how to get the request which brings your query, or how to
get the data from the database?

post your entire method (where you get the value of the request, and where
you get your data from the DB)
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jacob Arthur" <ja******@community.nospam> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
Just tried something a little different, still no luck.

I tried putting the line
SqlDataSource1.SelectCommand = docList;

In to the Page_Load event to make sure it was getting there and it did
during debugging, but there is no data being pulled back. I traded out
the command for

SqlDataSource1.SelectCommand = "select '1'";

and it still doesn't bring anything back in the datalist. Am I missing
something here possibly?

Jacob

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:uS*************@TK2MSFTNGP12.phx.gbl...
Hi,

Did you tried:

SelectCommand = Request.Form("hdnSelect").ToString()

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jacob Arthur" <ja******@community.nospam> wrote in message
news:e$**************@TK2MSFTNGP09.phx.gbl...
How would I go about using a custom select string that is passed from a
form to the SelectCommand parameter of SqlDataSource?

I tried:
SelectCommand = "<% Request.Form("hdnSelect") %>"

but I got an error about putting <% %> tags in a literal. I tried
taking out the quotes (the " ") and it didn't do any good. I'm trying
to use the built in ASP.NET 2.0 DataList control, but I haven't come up
with anything.

Thanks,
Jacob



Dec 23 '05 #6
In the code behind file:
static String docList;
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = docList;
}
protected void Button1_Click(object sender, EventArgs e)
{
docList = "";
String currDocId = "";
docList = "SELECT DocIdFld, NameFld FROM doctbl WHERE ";

System.Collections.Queue docIDQ = new System.Collections.Queue();
txtDocIDs.Text = txtDocIDs.Text + "\n";
for (int i = 0; i < txtDocIDs.Text.Length; i++)
{
if (Char.IsLetterOrDigit((char)txtDocIDs.Text[i]))
currDocId += (char)txtDocIDs.Text[i];
else if (currDocId != "")
{
docIDQ.Enqueue(currDocId);
currDocId = "";
}
}
if (docIDQ.ToArray().GetLength(0) != 0)
{
for (int i = 0; i < docIDQ.ToArray().GetLength(0); i++)
{
docList = docList + "DocIdFld = " +
((String)docIDQ.ToArray()[i]).ToString();
if (i + 1 < docIDQ.ToArray().GetLength(0))
{
docList = docList + " OR ";
}
}
}
hdnSelect.Value = docList;
Label1.Text = docList;
}

And in the main page file:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
</asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" >
</asp:SqlDataSource>

My query string is getting built fine. The string (docList) is actually
stored to a label which is visible for debugging purposes. If I go out to
Query Analyzer, it brings back the rows that I would expect with the exact
select string pasted into it. For some reason though, it never actually
brings any results back on the server. Do I need to add a
SqlDataSource1.Select() or something similar after changing the select
command in the code-behind file perhaps?

Thanks,
Jacob

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Oh**************@TK2MSFTNGP12.phx.gbl...
Hi,

is your problem in how to get the request which brings your query, or how
to get the data from the database?

post your entire method (where you get the value of the request, and where
you get your data from the DB)
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jacob Arthur" <ja******@community.nospam> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
Just tried something a little different, still no luck.

I tried putting the line
SqlDataSource1.SelectCommand = docList;

In to the Page_Load event to make sure it was getting there and it did
during debugging, but there is no data being pulled back. I traded out
the command for

SqlDataSource1.SelectCommand = "select '1'";

and it still doesn't bring anything back in the datalist. Am I missing
something here possibly?

Jacob

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:uS*************@TK2MSFTNGP12.phx.gbl...
Hi,

Did you tried:

SelectCommand = Request.Form("hdnSelect").ToString()

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jacob Arthur" <ja******@community.nospam> wrote in message
news:e$**************@TK2MSFTNGP09.phx.gbl...
How would I go about using a custom select string that is passed from a
form to the SelectCommand parameter of SqlDataSource?

I tried:
SelectCommand = "<% Request.Form("hdnSelect") %>"

but I got an error about putting <% %> tags in a literal. I tried
taking out the quotes (the " ") and it didn't do any good. I'm trying
to use the built in ASP.NET 2.0 DataList control, but I haven't come up
with anything.

Thanks,
Jacob



Dec 23 '05 #7
Hi Jacob,

From the code you provided, the problem seems due to the sequence you build
the select command string and set it to the SqlDataSource's Select Command.
For ASP.NET page, the "Page_Load" event always fire before other
control(like button)'s postback event, so if you generate the new Select
string in postback event, and only set it to SqlDataSource in "page_load",
the SqlDataSource's selectCommand is not updated correctly.... Also, after
you update the SqlDataSource's SelectCommand, we'd better recall the
DataBound control( GridView , DetailsView....)'s DataBind() method so that
they'll repopulate the datas from the DataSource so as to reflect the
changes.....

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Reply-To: "Jacob Arthur" <jacob-no@--spam-atstn.com>
| From: "Jacob Arthur" <ja******@community.nospam>
| References: <e$**************@TK2MSFTNGP09.phx.gbl>
<uS*************@TK2MSFTNGP12.phx.gbl>
<uQ**************@tk2msftngp13.phx.gbl>
<Oh**************@TK2MSFTNGP12.phx.gbl>
| Subject: Re: Select string building in C# and ASP.NET
| Date: Fri, 23 Dec 2005 11:50:12 -0600
| Lines: 133
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <ue*************@TK2MSFTNGP14.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.framework.aspnet,microsoft .public.dotnet.languages.c
sharp
| NNTP-Posting-Host: 015-952-822.area7.spcsdns.net 68.242.121.62
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:373896
microsoft.public.dotnet.framework.aspnet:366815
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| In the code behind file:
| static String docList;
| protected void Page_Load(object sender, EventArgs e)
| {
| SqlDataSource1.SelectCommand = docList;
| }
| protected void Button1_Click(object sender, EventArgs e)
| {
| docList = "";
| String currDocId = "";
| docList = "SELECT DocIdFld, NameFld FROM doctbl WHERE ";
|
| System.Collections.Queue docIDQ = new System.Collections.Queue();
| txtDocIDs.Text = txtDocIDs.Text + "\n";
| for (int i = 0; i < txtDocIDs.Text.Length; i++)
| {
| if (Char.IsLetterOrDigit((char)txtDocIDs.Text[i]))
| currDocId += (char)txtDocIDs.Text[i];
| else if (currDocId != "")
| {
| docIDQ.Enqueue(currDocId);
| currDocId = "";
| }
| }
| if (docIDQ.ToArray().GetLength(0) != 0)
| {
| for (int i = 0; i < docIDQ.ToArray().GetLength(0); i++)
| {
| docList = docList + "DocIdFld = " +
| ((String)docIDQ.ToArray()[i]).ToString();
| if (i + 1 < docIDQ.ToArray().GetLength(0))
| {
| docList = docList + " OR ";
| }
| }
| }
| hdnSelect.Value = docList;
| Label1.Text = docList;
| }
|
| And in the main page file:
| <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
| </asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server"
| ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
| ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" >
| </asp:SqlDataSource>
|
| My query string is getting built fine. The string (docList) is actually
| stored to a label which is visible for debugging purposes. If I go out
to
| Query Analyzer, it brings back the rows that I would expect with the
exact
| select string pasted into it. For some reason though, it never actually
| brings any results back on the server. Do I need to add a
| SqlDataSource1.Select() or something similar after changing the select
| command in the code-behind file perhaps?
|
| Thanks,
| Jacob
|
| "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
| in message news:Oh**************@TK2MSFTNGP12.phx.gbl...
| > Hi,
| >
| > is your problem in how to get the request which brings your query, or
how
| > to get the data from the database?
| >
| > post your entire method (where you get the value of the request, and
where
| > you get your data from the DB)
| >
| >
| > --
| > Ignacio Machin,
| > ignacio.machin AT dot.state.fl.us
| > Florida Department Of Transportation
| >
| >
| > "Jacob Arthur" <ja******@community.nospam> wrote in message
| > news:uQ**************@tk2msftngp13.phx.gbl...
| >> Just tried something a little different, still no luck.
| >>
| >> I tried putting the line
| >> SqlDataSource1.SelectCommand = docList;
| >>
| >> In to the Page_Load event to make sure it was getting there and it did
| >> during debugging, but there is no data being pulled back. I traded
out
| >> the command for
| >>
| >> SqlDataSource1.SelectCommand = "select '1'";
| >>
| >> and it still doesn't bring anything back in the datalist. Am I
missing
| >> something here possibly?
| >>
| >> Jacob
| >>
| >> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
| >> wrote in message news:uS*************@TK2MSFTNGP12.phx.gbl...
| >>> Hi,
| >>>
| >>> Did you tried:
| >>>
| >>> SelectCommand = Request.Form("hdnSelect").ToString()
| >>>
| >>>
| >>>
| >>> --
| >>> Ignacio Machin,
| >>> ignacio.machin AT dot.state.fl.us
| >>> Florida Department Of Transportation
| >>>
| >>>
| >>> "Jacob Arthur" <ja******@community.nospam> wrote in message
| >>> news:e$**************@TK2MSFTNGP09.phx.gbl...
| >>>> How would I go about using a custom select string that is passed
from a
| >>>> form to the SelectCommand parameter of SqlDataSource?
| >>>>
| >>>> I tried:
| >>>> SelectCommand = "<% Request.Form("hdnSelect") %>"
| >>>>
| >>>> but I got an error about putting <% %> tags in a literal. I tried
| >>>> taking out the quotes (the " ") and it didn't do any good. I'm
trying
| >>>> to use the built in ASP.NET 2.0 DataList control, but I haven't come
up
| >>>> with anything.
| >>>>
| >>>> Thanks,
| >>>> Jacob
| >>>>
| >>>
| >>>
| >>
| >>
| >
| >
|
|
|

Dec 26 '05 #8
Hi Jacob,

Any progress on this? If there're anything else we can help, please feel
free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 21370574
| References: <e$**************@TK2MSFTNGP09.phx.gbl>
<uS*************@TK2MSFTNGP12.phx.gbl>
<uQ**************@tk2msftngp13.phx.gbl>
<Oh**************@TK2MSFTNGP12.phx.gbl>
<ue*************@TK2MSFTNGP14.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Mon, 26 Dec 2005 02:00:54 GMT
| Subject: Re: Select string building in C# and ASP.NET
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <A$**************@TK2MSFTNGXA02.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 161
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:366996
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Jacob,
|
| From the code you provided, the problem seems due to the sequence you
build
| the select command string and set it to the SqlDataSource's Select
Command.
| For ASP.NET page, the "Page_Load" event always fire before other
| control(like button)'s postback event, so if you generate the new Select
| string in postback event, and only set it to SqlDataSource in
"page_load",
| the SqlDataSource's selectCommand is not updated correctly.... Also,
after
| you update the SqlDataSource's SelectCommand, we'd better recall the
| DataBound control( GridView , DetailsView....)'s DataBind() method so
that
| they'll repopulate the datas from the DataSource so as to reflect the
| changes.....
|
| Regards,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
|
|
|
| --------------------
| | Reply-To: "Jacob Arthur" <jacob-no@--spam-atstn.com>
| | From: "Jacob Arthur" <ja******@community.nospam>
| | References: <e$**************@TK2MSFTNGP09.phx.gbl>
| <uS*************@TK2MSFTNGP12.phx.gbl>
| <uQ**************@tk2msftngp13.phx.gbl>
| <Oh**************@TK2MSFTNGP12.phx.gbl>
| | Subject: Re: Select string building in C# and ASP.NET
| | Date: Fri, 23 Dec 2005 11:50:12 -0600
| | Lines: 133
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| | X-RFC2646: Format=Flowed; Response
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| | Message-ID: <ue*************@TK2MSFTNGP14.phx.gbl>
| | Newsgroups:
|
microsoft.public.dotnet.framework.aspnet,microsoft .public.dotnet.languages.c
| sharp
| | NNTP-Posting-Host: 015-952-822.area7.spcsdns.net 68.242.121.62
| | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| | Xref: TK2MSFTNGXA02.phx.gbl
| microsoft.public.dotnet.languages.csharp:373896
| microsoft.public.dotnet.framework.aspnet:366815
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| |
| | In the code behind file:
| | static String docList;
| | protected void Page_Load(object sender, EventArgs e)
| | {
| | SqlDataSource1.SelectCommand = docList;
| | }
| | protected void Button1_Click(object sender, EventArgs e)
| | {
| | docList = "";
| | String currDocId = "";
| | docList = "SELECT DocIdFld, NameFld FROM doctbl WHERE ";
| |
| | System.Collections.Queue docIDQ = new System.Collections.Queue();
| | txtDocIDs.Text = txtDocIDs.Text + "\n";
| | for (int i = 0; i < txtDocIDs.Text.Length; i++)
| | {
| | if (Char.IsLetterOrDigit((char)txtDocIDs.Text[i]))
| | currDocId += (char)txtDocIDs.Text[i];
| | else if (currDocId != "")
| | {
| | docIDQ.Enqueue(currDocId);
| | currDocId = "";
| | }
| | }
| | if (docIDQ.ToArray().GetLength(0) != 0)
| | {
| | for (int i = 0; i < docIDQ.ToArray().GetLength(0); i++)
| | {
| | docList = docList + "DocIdFld = " +
| | ((String)docIDQ.ToArray()[i]).ToString();
| | if (i + 1 < docIDQ.ToArray().GetLength(0))
| | {
| | docList = docList + " OR ";
| | }
| | }
| | }
| | hdnSelect.Value = docList;
| | Label1.Text = docList;
| | }
| |
| | And in the main page file:
| | <asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1">
| | </asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server"
| | ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
| | ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" >
| | </asp:SqlDataSource>
| |
| | My query string is getting built fine. The string (docList) is
actually
| | stored to a label which is visible for debugging purposes. If I go out
| to
| | Query Analyzer, it brings back the rows that I would expect with the
| exact
| | select string pasted into it. For some reason though, it never
actually
| | brings any results back on the server. Do I need to add a
| | SqlDataSource1.Select() or something similar after changing the select
| | command in the code-behind file perhaps?
| |
| | Thanks,
| | Jacob
| |
| | "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
| wrote
| | in message news:Oh**************@TK2MSFTNGP12.phx.gbl...
| | > Hi,
| | >
| | > is your problem in how to get the request which brings your query, or
| how
| | > to get the data from the database?
| | >
| | > post your entire method (where you get the value of the request, and
| where
| | > you get your data from the DB)
| | >
| | >
| | > --
| | > Ignacio Machin,
| | > ignacio.machin AT dot.state.fl.us
| | > Florida Department Of Transportation
| | >
| | >
| | > "Jacob Arthur" <ja******@community.nospam> wrote in message
| | > news:uQ**************@tk2msftngp13.phx.gbl...
| | >> Just tried something a little different, still no luck.
| | >>
| | >> I tried putting the line
| | >> SqlDataSource1.SelectCommand = docList;
| | >>
| | >> In to the Page_Load event to make sure it was getting there and it
did
| | >> during debugging, but there is no data being pulled back. I traded
| out
| | >> the command for
| | >>
| | >> SqlDataSource1.SelectCommand = "select '1'";
| | >>
| | >> and it still doesn't bring anything back in the datalist. Am I
| missing
| | >> something here possibly?
| | >>
| | >> Jacob
| | >>
| | >> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT
dot.state.fl.us>
| | >> wrote in message news:uS*************@TK2MSFTNGP12.phx.gbl...
| | >>> Hi,
| | >>>
| | >>> Did you tried:
| | >>>
| | >>> SelectCommand = Request.Form("hdnSelect").ToString()
| | >>>
| | >>>
| | >>>
| | >>> --
| | >>> Ignacio Machin,
| | >>> ignacio.machin AT dot.state.fl.us
| | >>> Florida Department Of Transportation
| | >>>
| | >>>
| | >>> "Jacob Arthur" <ja******@community.nospam> wrote in message
| | >>> news:e$**************@TK2MSFTNGP09.phx.gbl...
| | >>>> How would I go about using a custom select string that is passed
| from a
| | >>>> form to the SelectCommand parameter of SqlDataSource?
| | >>>>
| | >>>> I tried:
| | >>>> SelectCommand = "<% Request.Form("hdnSelect") %>"
| | >>>>
| | >>>> but I got an error about putting <% %> tags in a literal. I tried
| | >>>> taking out the quotes (the " ") and it didn't do any good. I'm
| trying
| | >>>> to use the built in ASP.NET 2.0 DataList control, but I haven't
come
| up
| | >>>> with anything.
| | >>>>
| | >>>> Thanks,
| | >>>> Jacob
| | >>>>
| | >>>
| | >>>
| | >>
| | >>
| | >
| | >
| |
| |
| |
|
|

Dec 28 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tcs | last post: by
4 posts views Thread by Jacob Arthur | last post: by
2 posts views Thread by johnhanis | last post: by
6 posts views Thread by alex.kemsley | last post: by
7 posts views Thread by php_mysql_beginer911 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.