how to set the value of the parameter for the SelectCommand?

P

phil

Hi,

The gridview linked to this datasource shows nothing. The problem is the
value of the variable 'a' which is not passed, i'm afraid ...
The code-behind contains the value to be passed:
public a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
a = "phil"
End Sub

the aspx contains:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=....."
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= @name">
<SelectParameters>
<asp:parameter Name="name" DefaultValue=a />
</SelectParameters>
</asp:SqlDataSource>

Thanks
Phil
 
P

PeterKellner

Hi,

The gridview linked to this datasource shows nothing. The problem is the
value of the variable 'a' which is not passed, i'm afraid ...
The code-behind contains the value to be passed:
public a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
a = "phil"
End Sub

the aspx contains:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=....."
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= @name">
<SelectParameters>
<asp:parameter Name="name" DefaultValue=a />
</SelectParameters>
</asp:SqlDataSource>

Thanks
Phil

don't you need quotes around DefaultValue?

Not sure what you are asking.


<SelectParameters>
<asp:parameter DefaultValue="'r'" Name="ShortCut"
Type="String" />
</SelectParameters>
Peter Kellner
http://peterkellner.net
 
W

William \(Bill\) Vaughn

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

PeterKellner said:
Hi,

The gridview linked to this datasource shows nothing. The problem is the
value of the variable 'a' which is not passed, i'm afraid ...
The code-behind contains the value to be passed:
public a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
a = "phil"
End Sub

the aspx contains:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=....."
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= @name">
<SelectParameters>
<asp:parameter Name="name" DefaultValue=a />
</SelectParameters>
</asp:SqlDataSource>

Thanks
Phil

don't you need quotes around DefaultValue?

Not sure what you are asking.


<SelectParameters>
<asp:parameter DefaultValue="'r'" Name="ShortCut"
Type="String" />
</SelectParameters>
Peter Kellner
http://peterkellner.net
 
W

William \(Bill\) Vaughn

You need several things:
1) A parameter marker in you query (SELECT statement) supported by the
provider. OLEDB does not support named parameters--so a "?" is probably
correct.
2) A Parameter object (member of the Parameters collection) that's
configured to match the attributes of your parameter (string, int, etc.).
3) Set the Parameter object Value property with the value to pass.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
P

phil

Hi,

What i mean is: is something like this possible (this gives no error but
also no gridview).
Variable 'a' gets its value in the code-behind, and pass variable 'a' to the
SelectCommand:
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= 'a' ">

I know you can do it: SqlDataSource1.SelectParameters.Add("name", a) but i
want to try it on a different way by minimizing the code in the code-behind
....
Thanks

William (Bill) Vaughn said:
You need several things:
1) A parameter marker in you query (SELECT statement) supported by the
provider. OLEDB does not support named parameters--so a "?" is probably
correct.
2) A Parameter object (member of the Parameters collection) that's
configured to match the attributes of your parameter (string, int, etc.).
3) Set the Parameter object Value property with the value to pass.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

phil said:
Hi,

The gridview linked to this datasource shows nothing. The problem is the
value of the variable 'a' which is not passed, i'm afraid ...
The code-behind contains the value to be passed:
public a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
a = "phil"
End Sub

the aspx contains:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=....."
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= @name">
<SelectParameters>
<asp:parameter Name="name" DefaultValue=a />
</SelectParameters>
</asp:SqlDataSource>

Thanks
Phil
 
W

William \(Bill\) Vaughn

I'm sorry I don't understand the issues--what problems you're trying to
solve. The code you included won't work...
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= ? ">

SqlDataSource1.SelectParameters.Add("MynameValue","a")

.... should work.


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

phil said:
Hi,

What i mean is: is something like this possible (this gives no error but
also no gridview).
Variable 'a' gets its value in the code-behind, and pass variable 'a' to
the
SelectCommand:
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= 'a' ">

I know you can do it: SqlDataSource1.SelectParameters.Add("name", a) but i
want to try it on a different way by minimizing the code in the
code-behind
...
Thanks

William (Bill) Vaughn said:
You need several things:
1) A parameter marker in you query (SELECT statement) supported by
the
provider. OLEDB does not support named parameters--so a "?" is probably
correct.
2) A Parameter object (member of the Parameters collection) that's
configured to match the attributes of your parameter (string, int, etc.).
3) Set the Parameter object Value property with the value to pass.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

phil said:
Hi,

The gridview linked to this datasource shows nothing. The problem is
the
value of the variable 'a' which is not passed, i'm afraid ...
The code-behind contains the value to be passed:
public a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
a = "phil"
End Sub

the aspx contains:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=....."
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]=
@name">
<SelectParameters>
<asp:parameter Name="name" DefaultValue=a />
</SelectParameters>
</asp:SqlDataSource>

Thanks
Phil
 
P

phil

Sorry if i'm not clear and thanks for your time anyway ..
Because it's possible to pass a variable from the code-behind to the body
tag of the aspx file (using the old <% %>), i thought it was also possible
to put directly the variable into the SelectCommand.
My mistake is that the variable is unknown in the tag <asp:sqldatasource >
unless defined as parameter ...
Phil


William (Bill) Vaughn said:
I'm sorry I don't understand the issues--what problems you're trying to
solve. The code you included won't work...
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= ? ">

SqlDataSource1.SelectParameters.Add("MynameValue","a")

... should work.


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

phil said:
Hi,

What i mean is: is something like this possible (this gives no error but
also no gridview).
Variable 'a' gets its value in the code-behind, and pass variable 'a' to
the
SelectCommand:
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]= 'a' ">

I know you can do it: SqlDataSource1.SelectParameters.Add("name", a) but i
want to try it on a different way by minimizing the code in the
code-behind
...
Thanks

William (Bill) Vaughn said:
You need several things:
1) A parameter marker in you query (SELECT statement) supported by
the
provider. OLEDB does not support named parameters--so a "?" is probably
correct.
2) A Parameter object (member of the Parameters collection) that's
configured to match the attributes of your parameter (string, int, etc.).
3) Set the Parameter object Value property with the value to pass.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Hi,

The gridview linked to this datasource shows nothing. The problem is
the
value of the variable 'a' which is not passed, i'm afraid ...
The code-behind contains the value to be passed:
public a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
a = "phil"
End Sub

the aspx contains:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=....."
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name],[city] FROM [mytable] where [name]=
@name">
<SelectParameters>
<asp:parameter Name="name" DefaultValue=a />
</SelectParameters>
</asp:SqlDataSource>

Thanks
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top