Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how to pass variable to SelectCommand of SqlDataSource?

Reply
Thread Tools

how to pass variable to SelectCommand of SqlDataSource?

 
 
phil
Guest
Posts: n/a
 
      06-02-2006
Hi,

I want to put a recordset in a gridview but i don't know how to pass the
value of the variable in the 'where' statement. The value of the variable is
set in the code-behind. i added a tag <selectParameters> but i don't know
which elements are needed and how...

This is my aspx file:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=c:\mydb.mdb"
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name], [city] where [name]= @na">
<SelectParameters>
<asp:ControlParameter
Name="??" PropertyName="???" Type="String" ControlID="??" />
</SelectParameters>
</asp:SqlDataSource>

The code-behind contains this:
Public na As String 'i defined it public to be able to pass it to
aspx file ??
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
na = "phil"
End Sub

Thanks for help
Phil


 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      06-02-2006
If the value of your parameter isn't able to be drawn automatically via one
of the built-in parameter types (like ControlParemeter, QueryStringParameter,
etc) you can always handle the SqlDataSOurce's pre-processing events such
as Selecting, Updating, Deleting and Inserting to manually change or add
a parameter to its SqlCommand.

<script runat="server">
protected void _data_Selecting(object sender, SqlDataSourceSelectingEventArgs
e)
{
e.Command.Parameters["bar"].Value = "oxox";
}
</script>

<asp:SqlDataSource runat="server" ID="_data"
SelectCommand="select * from foo where bar = @bar"
OnSelecting="_data_Selecting">
<SelectParameters>
<asparameter Name="bar" DefaultValue="xoxo" />
</SelectParameters>
</asp:SqlDataSource>


-Brock
http://staff.develop.com/ballen


> Hi,
>
> I want to put a recordset in a gridview but i don't know how to pass
> the value of the variable in the 'where' statement. The value of the
> variable is set in the code-behind. i added a tag <selectParameters>
> but i don't know which elements are needed and how...
>
> This is my aspx file:
> <asp:SqlDataSource ID="SqlDataSource1" runat="server"
> ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0 ;Data
> Source=c:\mydb.mdb"
> ProviderName="System.Data.OleDb"
> SelectCommand="SELECT [name], [city] where [name]= @na">
> <SelectParameters>
> <asp:ControlParameter
> Name="??" PropertyName="???" Type="String" ControlID="??" />
> </SelectParameters>
> </asp:SqlDataSource>
> The code-behind contains this:
> Public na As String 'i defined it public to be able to pass it
> to
> aspx file ??
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Handles Me.Load
> na = "phil"
> End Sub
> Thanks for help
> Phil



 
Reply With Quote
 
 
 
 
phil
Guest
Posts: n/a
 
      06-02-2006
Thanks, it works ...

"Brock Allen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> If the value of your parameter isn't able to be drawn automatically via

one
> of the built-in parameter types (like ControlParemeter,

QueryStringParameter,
> etc) you can always handle the SqlDataSOurce's pre-processing events such
> as Selecting, Updating, Deleting and Inserting to manually change or add
> a parameter to its SqlCommand.
>
> <script runat="server">
> protected void _data_Selecting(object sender,

SqlDataSourceSelectingEventArgs
> e)
> {
> e.Command.Parameters["bar"].Value = "oxox";
> }
> </script>
>
> <asp:SqlDataSource runat="server" ID="_data"
> SelectCommand="select * from foo where bar = @bar"
> OnSelecting="_data_Selecting">
> <SelectParameters>
> <asparameter Name="bar" DefaultValue="xoxo" />
> </SelectParameters>
> </asp:SqlDataSource>
>
>
> -Brock
> http://staff.develop.com/ballen
>
>
> > Hi,
> >
> > I want to put a recordset in a gridview but i don't know how to pass
> > the value of the variable in the 'where' statement. The value of the
> > variable is set in the code-behind. i added a tag <selectParameters>
> > but i don't know which elements are needed and how...
> >
> > This is my aspx file:
> > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
> > ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0 ;Data
> > Source=c:\mydb.mdb"
> > ProviderName="System.Data.OleDb"
> > SelectCommand="SELECT [name], [city] where [name]= @na">
> > <SelectParameters>
> > <asp:ControlParameter
> > Name="??" PropertyName="???" Type="String" ControlID="??" />
> > </SelectParameters>
> > </asp:SqlDataSource>
> > The code-behind contains this:
> > Public na As String 'i defined it public to be able to pass it
> > to
> > aspx file ??
> > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> > System.EventArgs)
> > Handles Me.Load
> > na = "phil"
> > End Sub
> > Thanks for help
> > Phil

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
SelectCommand that does not return any key column information Shenc ASP .Net 3 12-21-2012 03:24 PM
Selectcommand Variable Dave ASP .Net 1 05-16-2007 06:05 AM
spit out SelectCommand G. Whiz ASP .Net 5 03-07-2006 07:50 AM
Change selectcommand on click in aspx.vb prontoo ASP .Net 0 12-13-2005 03:30 PM



Advertisments