Show SQLDataSource ‘s SQL statement in ASP.NET
Posted: December 31, 2008 Filed under: ASP.NET 2 Leave a comment{
SqlParameterCollection myCollection = (SqlParameterCollection)e.Command.Parameters; IEnumerator ie = myCollection.GetEnumerator(); string strSQL = string.Empty; while ((ie.MoveNext())) { SqlParameter param = (SqlParameter)ie.Current; string strValue = string.Empty; if (param.Value ==null)strValue =
"NULL"; elsestrValue = param.Value.ToString();
strSQL += param.ParameterName.ToString() +
"=" + strValue + "<BR />";}
Response.Write(strSQL.ToString());
}
strSQL += param.ParameterName() &
"=" & param.Value() & "<BR />" End WhileResponse.Write(strSQL.ToString())
End Sub
View SQLDataSource’s SQL statement in ASP.NET
Posted: December 30, 2008 Filed under: ASP.NET Leave a commentFor i = 0 To e.Command.Parameters.Count – 1
mystring +=
"<Br />" + e.Command.Parameters(i).ToString() + ": " + e.Command.Parameters(i).Value.ToString()
Next
Me.lblDebug.Text = mystring.ToString
End Sub
C#:
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
string mystring=e.Command.CommandText.ToString();
for (int i = 0; i < e.Command.Parameters.Count; i++)
mystring += "<Br />" + e.Command.Parameters[i].ToString()
+ ": " + e.Command.Parameters[i].Value.ToString();
Response.Write(mystring);
}
Remove icon border shadows on the desktop
Posted: December 29, 2008 Filed under: My IT tools Leave a commentFilterExpression with multiple columns in SQLDataSource
Posted: December 17, 2008 Filed under: ASP.NET 2 2 Comments
Update:
–***
By assigning default values to controlparameters, we can make FilterExpresssin work with multiple columns with this syntax:
FilterExpression="(UserName =’{0}’ or ‘{0}’ =’-1′) AND (LiveMonth={1} or {1}=-1) AND (LiveYear={2} or {2}=-1) "
along with a default value -1 for all controlparameters.
–****
It is pretty easy to use a FilterExpression with SqlDataSource if you choose to filter the data after dataset returned through SELECT command. Normally, I would recommend to filter data from database call with a SelectParameter. When you decide to use multiple filterparameters, it seems that it will not that hard to add another FilterParameter to FilterParameters collection. However, after you add another parameter to the FilterExpression, the filtering simply stops working.
There is no syntax error for this behavior. A work around for this is to use some code to handle the fileterparameters andFilterExpression of the SqlDataSource.
Here is a working sample with three dropdownlist. The code is hooked to dropdownlist’s SelectedIndexChanged event.
protected void ddls_SelectedIndexChanged(object sender, System.EventArgs e)
{
bool bUsers = string.IsNullOrEmpty(ddlUsers.SelectedValue);
bool bMonth = string.IsNullOrEmpty(ddlMonth.SelectedValue);
bool bYear = string.IsNullOrEmpty(ddlYear.SelectedValue);
SqlDataSource1.FilterParameters.Clear();
SqlDataSource1.FilterExpression = "";
if (bUsers & bMonth & bYear){}
else if (!bUsers & bMonth & bYear)
{
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("UserName", TypeCode.String, "ddlUsers", "SelectedValue"));
SqlDataSource1.FilterExpression = "UserName=’{0}’";
}
else if (bUsers & !bMonth & bYear)
{
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveMonth", TypeCode.Int32, "ddlMonth", "SelectedValue"));
SqlDataSource1.FilterExpression = "LiveMonth={0}";
}
else if (bUsers & bMonth & !bYear)
{
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveMonth", TypeCode.Int32, "ddlYear", "SelectedValue"));
SqlDataSource1.FilterExpression = "LiveYear={0}";
}
else if (!bUsers & bMonth & !bYear)
{
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("UserName", TypeCode.String, "ddlUsers", "SelectedValue"));
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveYear", TypeCode.Int32, "ddlYear", "SelectedValue"));
SqlDataSource1.FilterExpression = "UserName=’{0}’ AND LiveYear={1}";
}
else if (!bUsers & !bMonth & bYear)
{
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("UserName", TypeCode.String, "ddlUsers", "SelectedValue"));
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveMonth", TypeCode.Int32, "ddlMonth", "SelectedValue"));
SqlDataSource1.FilterExpression = "UserName=’{0}’ AND LiveMonth={1}";
}
else if (bUsers & !bMonth & !bYear)
{
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveMonth", TypeCode.Int32, "ddlMonth", "SelectedValue"));
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveYear", TypeCode.Int32, "ddlYear", "SelectedValue"));
SqlDataSource1.FilterExpression = "LiveMonth={0} AND LiveYear={1}";
}
else if (!bUsers & !bMonth & !bYear)
{
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("UserName", TypeCode.String, "ddlUsers", "SelectedValue"));
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveMonth", TypeCode.Int32, "ddlMonth", "SelectedValue"));
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("LiveYear", TypeCode.Int32, "ddlYear", "SelectedValue"));
SqlDataSource1.FilterExpression = "UserName=’{0}’ AND LiveMonth={1} AND LiveYear={2}";
}
}
The aspx page looks like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="LiveMonth" HeaderText="LiveMonth" SortExpression="LiveMonth" />
<asp:BoundField DataField="dob" HeaderText="dob" SortExpression="dob" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"
CancelSelectOnNullParameter="true"
SelectCommand="SELECT dob, name, LiveYear, LiveMonth, [id], UserName FROM [Dates4] " >
</asp:SqlDataSource>
<asp:DropDownList ID="ddlUsers" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddls_SelectedIndexChanged">
<asp:ListItem Value="">All</asp:ListItem>
<asp:ListItem Value="aaa">aaa</asp:ListItem>
<asp:ListItem Value="bbb">bbb</asp:ListItem>
<asp:ListItem Value="ccc">ccc</asp:ListItem>
<asp:ListItem Value="ddd">ddd</asp:ListItem>
<asp:ListItem Value="xxx">xxx</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddls_SelectedIndexChanged">
<asp:ListItem Value="">All</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="11">11</asp:ListItem>
<asp:ListItem Value="12">12</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddls_SelectedIndexChanged">
<asp:ListItem Value="">All</asp:ListItem>
<asp:ListItem Value="2007">2007</asp:ListItem>
<asp:ListItem Value="2008">2008</asp:ListItem>
<asp:ListItem Value="2009">2009</asp:ListItem>
</asp:DropDownList>
Edit:
Sample aspx page::
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="LiveMonth" HeaderText="LiveMonth" SortExpression="LiveMonth" />
<asp:BoundField DataField="dob" HeaderText="dob" SortExpression="dob" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"
SelectCommand="SELECT dob, name, LiveYear, LiveMonth, [id], UserName FROM [Dates4] "
FilterExpression="(UserName =’{0}’ or ‘{0}’ =’-1′) AND (LiveMonth={1} or {1}=-1) AND (LiveYear={2} or {2}=-1) " >
<
FilterParameters>
<
asp:ControlParameter ControlID="ddlUsers" Name="UserName" PropertyName="SelectedValue" Type="String" DefaultValue="-1" />
<
asp:ControlParameter ControlID="ddlMonth" Name="LiveMonth" PropertyName="SelectedValue" Type="Int32" DefaultValue="-1" />
<
asp:ControlParameter ControlID="ddlYear" Name="LiveYear" PropertyName="SelectedValue" Type="Int32" DefaultValue="-1" />
</
FilterParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddlUsers" runat="server" AutoPostBack="true" >
<asp:ListItem Value="-1">All</asp:ListItem>
<asp:ListItem Value="aaa">aaa</asp:ListItem>
<asp:ListItem Value="bbb">bbb</asp:ListItem>
<asp:ListItem Value="ccc">ccc</asp:ListItem>
<asp:ListItem Value="ddd">ddd</asp:ListItem>
<asp:ListItem Value="xxx">xxx</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" >
<asp:ListItem Value="-1">All</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="11">11</asp:ListItem>
<asp:ListItem Value="12">12</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" >
<asp:ListItem Value="-1">All</asp:ListItem>
<asp:ListItem Value="2007">2007</asp:ListItem>
<asp:ListItem Value="2008">2008</asp:ListItem>
<asp:ListItem Value="2009">2009</asp:ListItem>
</asp:DropDownList>
Search Special character in SQL Server
Posted: December 9, 2008 Filed under: SQL Server Leave a comment
DECLARE @t TABLE (col1 varchar(50))
INSERT INTO @t (col1) VALUES (‘X/BCD11’)
INSERT INTO @t (col1) VALUES (‘D#FDR12’)
INSERT INTO @t (col1) VALUES (‘WB$CD13’)
INSERT INTO @t (col1) VALUES (‘CD EF13’)
INSERT INTO @t (col1) VALUES (‘XBCD13’)
INSERT INTO @t (col1) VALUES (‘A|CDE3’)
SELECT col1, Flag = CASE
WHEN col1 like ‘%[/ #$\|]%’
THEN ‘Found Special character!’
ELSE ‘Normal’
END,
Flag2 = CASE
WHEN PATINDEX(‘%[/ #$\|]%’, col1) > 0
THEN ‘Found Special character!’
ELSE ‘Normal’
END
FROM @t