Wednesday, February 23, 2011

How to: Enable User Password Recovery Using the ASP.NET PasswordRecovery Control

How to: Enable User Password Recovery Using the ASP.NET PasswordRecovery Control
How to: Install and Configure SMTP Virtual Servers in IIS 6.0
Solving exceptions when doing password recovery

asp.net - Hashed passwords and PasswordRecovery control - Stack Overflow

asp.net - Hashed passwords and PasswordRecovery control - Stack Overflow: "UPDATE:

1) For some reason it works now. Namely, if we set requiresQuestionAndAnswer to false, then PR also sends email to firstUser


2) If passwords are stored in hashed form, then if:

a) enablePasswordRetrieval='true' and enablePasswordReset is set to either true or false --> PR generates exception
b) if enablePasswordRetrieval='false' and enablePasswordReset='false' --> PR generates exception
c) if enablePasswordRetrieval is set to false and enablePasswordReset is set to true, then PR automatically generates new pwd and emails it.


Similarly, if pwd is not hashed, but we have enablePasswordRetrieval='false', then enablePasswordReset must be set to true (so that PR generates a new pwd and emails it), else we get an exception"

Tuesday, February 22, 2011

How to: Read Connection Strings from the Web.config File

using System.Configuration;
using System.Data;

public static string GetUserName(string userid){
string fullname = "Unknown name";
string connString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; string sqlString = "SELECT * FROM Users WHERE UserID=@UserID";
SqlDataSource sqldatasource = new SqlDataSource(connString, sqlString);
sqldatasource.SelectParameters.Add(new Parameter("UserID", System.Data.DbType.String, userid));

DataView dv = (DataView)sqldatasource.Select(DataSourceSelectArguments.Empty);
if (dv != null && dv.Table != null & dv.Table.Rows.Count > 0)
{
fullname = dv.Table.Rows[0]["FullName"].ToString();
}

return fullname;
}

How to: Read Connection Strings from the Web.config File: "ConnectionStrings.ConnectionStrings['NorthwindConnectionString'];"

Monday, February 14, 2011

MembershipUser Class (System.Web.Security)

MembershipUser Class (System.Web.Security)

Using ConfigurationManager.AppSettings

Step 1
======
Add a reference to assembly


Step 2
======
Add this before the class header

using System.Configuration;

Step 3
======
Access the configuration setting using this code:

System.Configuration.ConfigurationManager.AppSettings["WebMasterEmail"]

Wednesday, February 2, 2011

The Search Form (Code Behind)

 

 protected void ButtonSearch_Click(object sender, EventArgs e)
{
string criterias = "";

criterias = TextBoxKeyword.Text.Length > 0 ? (criterias + "Keyword") : criterias;
criterias = TextBoxCourse.Text.Length > 0 ? (criterias + "Course") : criterias;
criterias = TextBoxStartDate.Text.Length > 0 ? (criterias + "EventDate") : criterias;
criterias = TextBoxDateOfEntryStart.Text.Length > 0 ? (criterias + "DateOfEntry") : criterias;


string keyword = TextBoxKeyword.Text.Length>0? TextBoxKeyword.Text : "NULL";
string course = TextBoxCourse.Text.Length > 0 ? TextBoxCourse.Text : "NULL";
string startdate = TextBoxStartDate.Text.Length > 0 ? TextBoxStartDate.Text : "NULL";
string enddate = TextBoxEndDate.Text.Length > 0 ? TextBoxEndDate.Text : "NULL";
string dateofentrystart = TextBoxDateOfEntryStart.Text.Length > 0 ? TextBoxDateOfEntryStart.Text : "NULL";
string dateofentryend = TextBoxDateOfEntryEnd.Text.Length > 0 ? TextBoxDateOfEntryEnd.Text : "NULL";

TextBox1.Text = "Criterias:" + criterias + "\nKeyword:" + keyword + "\nCourse:" + course
+ "\nStartDate:" + startdate + "\nEndDate:" + enddate
+ "\nDateOfEntryStart:" + dateofentrystart + "\nDateOfEntryEnd:" + dateofentryend;

SqlDataSourceEventsByCriteria.SelectParameters["Criteria"].DefaultValue = criterias;
SqlDataSourceEventsByCriteria.SelectParameters["Keyword"].DefaultValue = keyword;
SqlDataSourceEventsByCriteria.SelectParameters["Course"].DefaultValue = course;
SqlDataSourceEventsByCriteria.SelectParameters["EventStartDate"].DefaultValue = startdate;
SqlDataSourceEventsByCriteria.SelectParameters["EventEndDate"].DefaultValue = enddate;
SqlDataSourceEventsByCriteria.SelectParameters["DateOfEntryStart"].DefaultValue = dateofentrystart;
SqlDataSourceEventsByCriteria.SelectParameters["DateOfEntryEnd"].DefaultValue = dateofentryend;

try
{

DataView dvEvents = (DataView)SqlDataSourceEventsByCriteria.Select(DataSourceSelectArguments.Empty);
DataTable dtEvents = dvEvents.Table;
GridView1.DataSource = dtEvents;
GridView1.DataBind();
}
catch (Exception exSelect)
{
TextBox1.Text = exSelect.Message;
}
}

The Search Form

 

Remember to add the AjaxControlToolkit as reference first.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="TestSearch.aspx.cs" Inherits="Track_TestSearch" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
Keyword
<asp:TextBox ID="TextBoxKeyword" runat="server"></asp:TextBox>
<br />
Course
<asp:TextBox ID="TextBoxCourse" runat="server"></asp:TextBox>
<br />
Start Date
<asp:TextBox ID="TextBoxStartDate" runat="server"></asp:TextBox>
<asp:CalendarExtender
ID="CalendarExtender1" runat="server" TargetControlID="TextBoxStartDate"
Format="dd-MMM-yyyy">
</asp:CalendarExtender>
&nbsp;&nbsp;&nbsp;&nbsp;
End Date
<asp:TextBox ID="TextBoxEndDate" runat="server"></asp:TextBox>
<br />
<br />
<asp:CalendarExtender
ID="CalendarExtender2" runat="server" TargetControlID="TextBoxEndDate"
Format="dd-MMM-yyyy">
</asp:CalendarExtender>

<br />
Start Date
<asp:TextBox ID="TextBoxDateOfEntryStart" runat="server"></asp:TextBox>
<asp:CalendarExtender
ID="CalendarExtender3" runat="server" TargetControlID="TextBoxDateOfEntryStart"
Format="dd-MMM-yyyy">
</asp:CalendarExtender>
&nbsp;&nbsp;&nbsp;&nbsp;
End Date
<asp:TextBox ID="TextBoxDateOfEntryEnd" runat="server"></asp:TextBox>
<br />
<br />
<asp:CalendarExtender
ID="CalendarExtender4" runat="server" TargetControlID="TextBoxDateOfEntryEnd"
Format="dd-MMM-yyyy">
</asp:CalendarExtender>

<asp:TextBox ID="TextBox1" Rows="15" Cols="100" TextMode="MultiLine" runat="server"></asp:TextBox>

<asp:Button ID="ButtonSearch" runat="server" Text="Search"
onclick="ButtonSearch_Click" />

<br />

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSourceEventsByCriteria" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="GetEventsByCriteria6" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="Criteria" Type="String" />
<asp:Parameter Name="Keyword" Type="String" />
<asp:Parameter Name="Course" Type="String" />
<asp:Parameter Name="EventStartDate" Type="String" />
<asp:Parameter Name="EventEndDate" Type="String" />
<asp:Parameter Name="DateOfEntryStart" Type="String" />
<asp:Parameter Name="DateOfEntryEnd" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>

Generic Dynamically Constructed SQL string

 

USE [MyDB]
GO
/****** Object: StoredProcedure [dbo].[GetEventsByCriteria] Script Date: 02/02/2011 01:32:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET DATEFORMAT DMY
GO
create PROCEDURE [dbo].[GetEventsByCriteria6]
(
@Criteria nvarchar(255),
@Keyword nvarchar(255),
@Course nvarchar(255),
@EventStartDate nvarchar(25),
@EventEndDate nvarchar(25),
@DateOfEntryStart nvarchar(25),
@DateOfEntryEnd nvarchar(25)
)

As
DECLARE @SQLString NVARCHAR(500)
DECLARE @SQLStr1 NVARCHAR(200)
DECLARE @SQLStr2 NVARCHAR(200)
DECLARE @SQLStr3 NVARCHAR(200)
DECLARE @SQLStr4 NVARCHAR(200)

/* Set column list. CHAR(13) is a carriage return, line feed.*/
SET @SQLString = N'SELECT * FROM [EVENTS] ' + CHAR(13)

/* Set WHERE clause. */
SET @SQLStr1 = N' [EventStartDate]>=''' + @EventStartDate + ''''
+ N' AND [EventEndDate]<=''' + @EventEndDate + ''''


SET @SQLStr2 = N' [DateOfEntry]>=''' + @DateOfEntryStart + ''''
+ N' AND DateOfEntry<=''' + @DateOfEntryEnd+ ''''

SET @SQLStr3 = N' [EventTitle] LIKE ''%' + @Keyword + N'%'''

SET @SQLStr4 = N' [Course] LIKE ''%' + @Course + N'%'''


/* Set ORDER clause. */
if (charindex('WHERE',@SQLString)=0 and charindex('EventDate',@Criteria)<>0)
SET @SQLString = @SQLString + N' WHERE ' + @SQLStr1
else if (charindex('WHERE',@SQLString)<>0 and charindex('EventDate',@Criteria)<>0)
SET @SQLString = @SQLString + N' AND ' + @SQLStr1

if (charindex('WHERE',@SQLString)=0 and charindex('DateOfEntry',@Criteria)<>0)
SET @SQLString = @SQLString + N' WHERE ' + @SQLStr2
else if (charindex('WHERE',@SQLString)<>0 and charindex('DateOfEntry',@Criteria)<>0)
SET @SQLString = @SQLString + N' AND ' + @SQLStr2

if (charindex('WHERE',@SQLString)=0 and charindex('Keyword',@Criteria)<>0)
SET @SQLString = @SQLString + N' WHERE ' + @SQLStr3
else if (charindex('WHERE',@SQLString)<>0 and charindex('Keyword',@Criteria)<>0)
SET @SQLString = @SQLString + N' AND ' + @SQLStr3

if (charindex('WHERE',@SQLString)=0 and charindex('Course',@Criteria)<>0)
SET @SQLString = @SQLString + N' WHERE ' + @SQLStr4
else if (charindex('WHERE',@SQLString)<>0 and charindex('Course',@Criteria)<>0)
SET @SQLString = @SQLString + N' AND ' + @SQLStr4

SET @SQLString = @SQLString + N' ORDER BY [EventStartDate] DESC'
-- CONVERT(datetime, @eventstartdate, 103)

print @SQLString
EXEC sp_executesql @SQLString
--CONVERT(varchar(8), ctdate, 112)

Tuesday, February 1, 2011

Building Statements at Run Time

Building Statements at Run Time: "EXEC sp_executesql @SQLString GO"

USE [MyDB]
SET DATEFORMAT DMY
GO
/****** Object:  StoredProcedure [dbo].[GetEventsByCriteria]    Script Date: 02/01/2011 21:01:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[GetEventsByCriteria]
(
  @EventStartDate nvarchar(25)
)

As
DECLARE @SQLString NVARCHAR(500)


/* Set column list. CHAR(13) is a carriage return, line feed.*/
SET @SQLString = N'SELECT * FROM EVENTS ' + CHAR(13)

/* Set WHERE clause. */
SET @SQLString = @SQLString + N' WHERE EventStartDate>=''' + @EventStartDate + ''''
-- CONVERT(datetime, @eventstartdate, 103)

/* Set ORDER clause. */
SET @SQLString = @SQLString + N' ORDER BY EventStartDate DESC'
-- CONVERT(datetime, @eventstartdate, 103)

EXEC sp_executesql @SQLString
--CONVERT(varchar(8), ctdate, 112)