Archive

Archive for September, 2012

Change the UserName in Membership

September 29, 2012 Leave a comment

Hi all,

try this example to change username in Membership

1) Open VS2008 and create a new website and add new web page

2) From Toolbox drag TextBox, Lable, and Button Controls

3) In Code behind add the following code:


protected void btnChangeUsername_Click(object sender, EventArgs e)
 {
 string strNewUserName = txtNewUserName.Text.Trim();
 MembershipUser usr = Membership.GetUser(strNewUserName);
 if (strNewUserName == null)
 {
 Label1.Text = "You have to pick a username";
 }
 else
 {
 ChangeUsername(strNewUserName);
 }
 }

protected void ChangeUsername(string newUsername)
 {
 SqlConnection conn = null;
 string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 conn = new SqlConnection(connString);
 SqlCommand cmd = new SqlCommand("ChangeUsername", conn);
 cmd.CommandType = CommandType.StoredProcedure;
 string oldUsername = User.Identity.Name;
 conn.Open();
 cmd.Parameters.AddWithValue("@ApplicationName", Membership.ApplicationName);
 cmd.Parameters.AddWithValue("@OldUserName", oldUsername);
 cmd.Parameters.AddWithValue("@NewUserName", newUsername);
 SqlParameter retValParam = new SqlParameter("@ReturnValue", SqlDbType.Int);
 retValParam.Direction = ParameterDirection.ReturnValue;
 cmd.Parameters.Add(retValParam);
 cmd.ExecuteNonQuery();
 conn.Close();

int returnValue = -1;
 if (retValParam.Value != null)
 {
 returnValue = Convert.ToInt32(retValParam.Value);
 }

if (returnValue != 0)
 {
 Label1.Text = "There were problems. We were unable to change your username.";
 }
 else
 {
 FormsAuthentication.SetAuthCookie(newUsername, false);
 Response.Redirect("profile.aspx");
 }
 }

4) the stored procedure will be


CREATE PROCEDURE [dbo].[ChangeUsername]
 @ApplicationName nvarchar(256),
 @OldUserName nvarchar(256),
 @NewUserName nvarchar(256)
AS
BEGIN

-- Get the UserId and ApplicationId for the user
 DECLARE @UserId uniqueidentifier, @ApplicationId uniqueidentifier
 SELECT @UserId = NULL

 SELECT @UserId = u.UserId, @ApplicationId = a.ApplicationId
 FROM dbo.aspnet_Users u, dbo.aspnet_Applications a
 WHERE LoweredUserName = LOWER(@OldUserName) AND
 u.ApplicationId = a.ApplicationId AND
 LOWER(@ApplicationName) = a.LoweredApplicationName

IF (@UserId IS NULL)
 RETURN(1)

-- Ensure that @NewUserName is not in use
 IF (EXISTS(SELECT 1 FROM aspnet_Users WHERE LoweredUserName = LOWER(@NewUserName) AND ApplicationId = @ApplicationId))
 RETURN(2)

-- Change the username
 UPDATE aspnet_Users SET
 UserName = @NewUserName,
 LoweredUserName = LOWER(@NewUserName)
 FROM aspnet_Users
 WHERE UserId = @UserId AND ApplicationId = @ApplicationId

 -- Change email address
 UPDATE dbo.aspnet_Membership
 SET Email = LOWER(@NewUserName), LoweredEmail = LOWER(@NewUserName)
 WHERE UserId = @UserId AND ApplicationId = @ApplicationId

RETURN(0)
END

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net

Disabling Past Dates in AJAX Calendar

September 13, 2012 Leave a comment

Hi all,

try this example to disable past dates in AJAX Calender

1) Open VS2008 and create a new website

2) Add New Web Page and from Toolbox drag ScriptManager , TextBox and AJAX Calender Controls

3) Add This Javascript function


<script language="javascript" type="text/javascript">
 function CheckForPastDate(sender, args) {
 var selectedDate = new Date();
 selectedDate = sender._selectedDate;
 var todayDate = new Date();
 if (selectedDate.getDateOnly() < todayDate.getDateOnly()) {
 sender._selectedDate = todayDate;
 sender._textbox.set_Value(sender._selectedDate.format(sender._format));
 alert("Date Cannot be in the past.");
 }
 }
 </script>

4) Your Page will be like that


<div>
 Select Date :
 <asp:TextBox ID="txt_Date" runat="server"></asp:TextBox>
 <cc1:CalendarExtender ID="txt_Date_CalendarExtender" runat="server"
 Enabled="True" TargetControlID="txt_Date" OnClientDateSelectionChanged="CheckForPastDate">
 </cc1:CalendarExtender>
 </div>

Hope this helps

Good Luck

Categories: AJAX, ASP.Net, Javascript