RE: Default Value for Where Clause in Datasource



Hi Lee,

Welcome to MSDN Managed Newsgroup!

As Bob and William suggested, this newsgroup is for ADO specific questions.

You only mentioned DataGrid, I cannot tell if it's ASP.NET related or
WinForm related (since both have a DataGrid control but with different
data-binding mechanism).

I happen to be monitoring both this newsgroup and some ASP.NET related
newsgroups, therefore I can provide some general suggestion here if you
were using ASP.NET DataGrid (though I also suggest you to post in .NET
related newsgroups for the benefit of community, you can find all MSDN
Managed Newsgroups here:
http://msdn.microsoft.com/subscriptions/managednewsgroups/list.aspx).

Based on my understanding, if you use a select parameter in a SqlDataSource
control and you didn't provide a value for the parameter, the result will
be returnning all rows by default:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<html xmlns="http://www.w3.org/1999/xhtml"; >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="grid1" runat="server" DataSourceID="SqlDataSource1">
</asp:DataGrid>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [address]
FROM [authors] WHERE ([address] LIKE '%' + @address + '%')"
OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter DefaultValue="abc" Name="address" Type="String"
/>
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@address"].Value = TextBox1.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
grid1.DataBind();
}
}


Sincerely,
Walter Wang (wawang@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

.