My article that will explain how to create event for a user control can be found here http://tinyurl.com/yhwd9xw
In one of my projects I found this error Inconsistent accessibility: return type...................... is less accessible than method ......... It was a quite strange error for me as I have not seen the same in past. My classes were defined as:
class MyClass1
{}
class MyClass2
{}
class MyClass3
{}
Solution to this problem is very easy, all I did is add public with all my classes.
public class MyClass1
{}
public class MyClass2
{}
public class MyClass3
{}
And once again, the day is saved by "Powerpuf Girls", I mean by adding public :).
I was working with one of my projects in which I had to use Membership/Role providers. I run the wizard using aspnet_regsql.exe to create the structure of SqlMembershipProvider. I added few roles and users using ASP.Net Configuration. Everything worked fine tested my application multiple times and it worked very well. Then I generated the script from my local database for SqlMembershipProvider since we can not run the utility aspnet_regsql.exe on our test server. Everything went well so far. All the tables, views, stored procedures, roles were created and permissions were granted. Then when I try to run my application from the test server it gave me following error.
"The 'System.Web.Security.SqlMembershipProvider' requires a database schema compatible with schema version '1'. However, the current database schema is not compatible with this version. You may need to either install a compatible schema with aspnet_regsql.exe (available in the framework installation directory), or upgrade the provider to a newer version."
This was a bit confusing error for me since I had worked with SqlMembershipProvider in the past. Then I start recalling the difference between my previous and current deployment and the difference was in ealier deployments either I used aspnet_regsql.exe or copied the structure with data but this time I created the script and generate the structure.
Still no sign for me to detect the cause of error. Then I started checking the tables and I found that when we run aspnet_regsql.exe it not only creates the structure but insert some values as well in "aspnet_SchemaVersions" table which were missing in my test server. So created manual insert script to add following values.
-- --Table: [dbo].[aspnet_SchemaVersions]
-- --Insert
INSERT INTO [dbo].[aspnet_SchemaVersions] ([Feature], [CompatibleSchemaVersion], [IsCurrentVersion]) VALUES(N'common', N'1', 1)
INSERT INTO [dbo].[aspnet_SchemaVersions] ([Feature], [CompatibleSchemaVersion], [IsCurrentVersion]) VALUES(N'health monitoring', N'1', 1)
INSERT INTO [dbo].[aspnet_SchemaVersions] ([Feature], [CompatibleSchemaVersion], [IsCurrentVersion]) VALUES(N'membership', N'1', 1)
INSERT INTO [dbo].[aspnet_SchemaVersions] ([Feature], [CompatibleSchemaVersion], [IsCurrentVersion]) VALUES(N'personalization', N'1', 1)
INSERT INTO [dbo].[aspnet_SchemaVersions] ([Feature], [CompatibleSchemaVersion], [IsCurrentVersion]) VALUES(N'profile', N'1', 1)
INSERT INTO [dbo].[aspnet_SchemaVersions] ([Feature], [CompatibleSchemaVersion], [IsCurrentVersion]) VALUES(N'role manager', N'1', 1)
BINGO!!! After inserting these values my test server started working as good as my local machine :).
In one of my projects I have been asked to implement temporary security on sepcific module for internal usage and it was also suggested that I will not be putting more efforts on the same. First thing came in my mind was Membership/RoleProvider, but as I advised my planned solution they surprised me with the decision that I will not be using these providers and I should be using something more simpler and I was like Duh!!!
Any way I started thinking about a much more simpler solution as I have been alotted the biggest time period ever of 10-15 minutes max for this task :).
OK now I guess I should come to the point. During this process I realised that web.config credentials can be a good option as I personally don't wanted to implement any hardcoded authentication. It is very straight forward and simple but I had some problems implementing that solution so I thought to blog about how to authenticate using credentials incase if any one require the same.
All you need to do is to add some credentials in your web.config like this:
<authentication mode="Forms">
<forms loginUrl="~/SignIn.aspx" name=".ASPXAUTH" slidingExpiration="true" timeout="1440" path="/" defaultUrl="~/Default.aspx">
<credentials passwordFormat="Clear">
<user name="testUser1" password="testPass1"/>
<user name="testUser2" password="testPass2"/>
<user name="testUser2" password="testPass3"/>
</credentials>
</forms>
</authentication>
In the code block it can be authenticated like this:
if (FormsAuthentication.Authenticate(this.txtUserName.Text, this.txtPassword.Text))
{
FormsAuthentication.SetAuthCookie(this.txtUserName.Text, false);
FormsAuthentication.RedirectFromLoginPage(this.txtUserName.Text, false);
}
else
{
Response.Write("Invalid login details. Please try again.");
}
I hope it will be helpful for others as well. And yes it can be done in 15 minutes :)
Today I just found that my friend Google launched its web browser named Chrome which is suppose to be availble for download on 2nd Sep 2008 from this link Google Chrome, but unfortunately for some unknown good reason I am unable to download this guy, as whenever I click the link it takes me to Google home page. However I felt a little better when I successfully opened the ebook of Chrome after tearing off my hair because of the download link.
Now I am sure that almost everyone try to download this browser in curiosity as what Google launched after such a long time. Well why I kept the title of this blog as Another browser in the row because being a Software Engineer I will have to test my applications with one more browser. I hope we will not be having javascript and layout issues in this one and everything will work smoothly.
By the way you should check out the ebook link of Chrome as they talked about this new guy in a comic way and it looks quite catchy in this way.
Ahann!!! finally while writting this blog I found this blog link http://uzeeinc.wordpress.com/2008/09/02/download-google-chrome-browser/ and now I came to know that unknown good reason why I couldn't download this guy.
Some time back on the form somebody was looking for some help in searching URL within text and make those URLs as link. Me and that guy tried various regex but the one that worked out I thought to put it on the blog so that it can help me and others later. Regex itself is:
-------- In VB.Net ---------
Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)
-------- In C#.Net ---------
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
And I used following method to convert the URLs into link within text.
-------- In VB.Net ---------
Protected Function MakeLink(ByVal txt As String) As String
Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)
Dim mactches As MatchCollection = regx.Matches(txt)
For Each match As Match In mactches
txt = txt.Replace(match.Value, "<a href='" & match.Value & "'>" & match.Value & "</a>")
Next
Return txt
End Function
------- In C#.Net --------
protected string MakeLink(string txt)
{
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(txt);
foreach (Match match in mactches) {
txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
}
return txt;
}
Since long I was wondering on how I will be able to use FileUpload control inside UpdatePanel and I am sure there will be lot of other people who were expecting the same to be working. I found an intersting video article by Joe Stagner in which he described how to use the FileUpload control inside iframe to give some AJAX effect. You can find this video here. http://www.asp.net/learn/videos/video-254.aspx
I response to one of my blog post entry I received few queries regarding mutually exclusive checkbox within GirdView, meaning if there are two checkboxes in a row only one can be selected at a time. If checkbox1 is selected and you select checkbox2 then checkbox1 should be deselected. Following javascript and html can be used.
<script type="text/javascript">
function checkMutuallyExclusive(target)
{
document.getElementById(target).checked = false;
}
</script>
.
.
.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">
<Columns>
<asp:TemplateField>
<AlternatingItemTemplate>
<asp:CheckBox ID="cbStatus1" runat="server" />
</AlternatingItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbStatus1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
AlternatingItemTemplate>
<asp:CheckBox ID="cbStatus2" runat="server" />
</AlternatingItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbStatus2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
----------------------------------.vb file if VB.Net is the language ------------------------------------
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
DirectCast(e.Row.FindControl("cbStatus1"), CheckBox).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & DirectCast(e.Row.FindControl("cbStatus2"), CheckBox).ClientID & "')")
DirectCast(e.Row.FindControl("cbStatus2"), CheckBox).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & DirectCast(e.Row.FindControl("cbStatus1"), CheckBox).ClientID & "')")
End If
End Sub
----------------------------------.cs file if C#.Net is the language ------------------------------------
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow)) {
((CheckBox)e.Row.FindControl("cbStatus1")).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + ((CheckBox)e.Row.FindControl("cbStatus2")).ClientID + "')");
((CheckBox)e.Row.FindControl("cbStatus2")).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + ((CheckBox)e.Row.FindControl("cbStatus1")).ClientID + "')");
}
}
Different forums are filled with the questions regarding how to manually implement cookies for login or in other words how to implement "Remeber me" option.
Following is the code that will give the idea of how to achieve this task.
Controls used
1. TextBox, ID = TbUserName
2. TextBox, ID = TbPassword
3. CheckBox, ID = CbRememberMe
4. Button, ID = BtLogin
5. LinkButton, ID = lbSignout
------------------If you are using VB.Net-------------------------
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'Check if the browser support cookies
If Request.Browser.Cookies Then
'Check if the cookies with name PBLOGIN exist on user's machine
If Request.Cookies("PBLOGIN") IsNot Nothing Then
'Pass the user name and password to the VerifyLogin method
Me.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString())
End If
End If
End If
End Sub
Protected Sub BtLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'check if remember me checkbox is checked on login
If (Me.CbRememberMe.Checked) Then
'Check if the browser support cookies
If (Request.Browser.Cookies) Then
'Check if the cookie with name PBLOGIN exist on user's machine
If (Request.Cookies("PBLOGIN") Is Nothing) Then
'Create a cookie with expiry of 30 days
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30)
'Write username to the cookie
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
'Write password to the cookie
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
'If the cookie already exist then wirte the user name and password on the cookie
Else
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
End If
End If
End If
Me.VerifyLogin(Me.TbUserName.Text, Me.TbPassword.Text)
End Sub
Protected Sub VerifyLogin(ByVal UserName As String, ByVal Password As String)
Try
'If login credentials are correct
'Redirect to the user page
'else
'prompt user for invalid password
'end if
Catch ex as System.Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub lbSignout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbSignout.Click
'Check iIf the cookies with name PBLOGIN exist on user's machine
If (Request.Cookies("PBLOGIN") IsNot Nothing) Then
'Expire the cookie
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30)
End If
'Redirect to the login page
End Sub
End Class
------------------If you are using C#.Net-------------------------
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
//Check if the browser support cookies
if (Request.Browser.Cookies)
{
//Check if the cookies with name PBLOGIN exist on user's machine
if (Request.Cookies("PBLOGIN") != null)
{
//Pass the user name and password to the VerifyLogin method
this.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString());
}
}
}
}
protected void BtLogin_Click(object sender, System.EventArgs e)
{
//check if remember me checkbox is checked on login
if ((this.CbRememberMe.Checked))
{
//Check if the browser support cookies
if ((Request.Browser.Cookies))
{
//Check if the cookie with name PBLOGIN exist on user's machine
if ((Request.Cookies("PBLOGIN") == null))
{
//Create a cookie with expiry of 30 days
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30);
//Write username to the cookie
Response.Cookies("PBLOGIN").Item("UNAME") = this.TbUserName.Text;
//Write password to the cookie
Response.Cookies("PBLOGIN").Item("UPASS") = this.TbPassword.Text;
}
//If the cookie already exist then wirte the user name and password on the cookie
else
{
Response.Cookies("PBLOGIN").Item("UNAME") = this.TbUserName.Text;
Response.Cookies("PBLOGIN").Item("UPASS") = this.TbPassword.Text;
}
}
}
this.VerifyLogin(this.TbUserName.Text, this.TbPassword.Text);
}
protected void VerifyLogin(string UserName, string Password)
{
try
{
//If login credentials are correct
//Redirect to the user page
//else
//prompt user for invalid password
//end if
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}
}
protected void lbSignout_Click(object sender, System.EventArgs e)
{
//Check iIf the cookies with name PBLOGIN exist on user's machine
if ((Request.Cookies("PBLOGIN") != null))
{
//Expire the cookie
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30);
}
//Redirect to the login page
}
}
If you want to add an option that users can click a button on your website and it will open up bookmark option. Following is the code that will perform this task.
<html>
<body>
<SCRIPT LANGUAGE="JavaScript">
function bookmark(url, description)
{
if (navigator.appName=='Microsoft Internet Explorer')
{
window.external.AddFavorite(url, description);
}
else
{
alert('This option works with IE only as of now.');
}
}
</SCRIPT>
<input type="Button" ID="btnBookMark" onClick="bookmark('http://mypage.aspx','MyWebsite')" value="Bookmark" />
</body>
</html>
More Posts
Next page »