Could not load file or assembly 'System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencie

In this tutorial you will learn how to get rid of this Could not load file or assembly 'System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified error. Today when I tried to run one of my .aspx page in my web project while using visual studio 2008, I got this error. In my this .aspx page I am using JSON (JavaScript Object Notation) and for JSON to work properly I was using System.Web.Services namespace.


I am giving all these details because I was not getting such error when I tried to run any of my .aspx webpage that was not using JSON technology. So lets have a look over how to solve this problem.


You just have to install .NET Framework 3.5 Service Pack and that's it. You can download the .NET Framework 3.5 Service Pack 1 microsoft website using this link http://www.microsoft.com/downloads/details.aspx?FamilyID=AB99342F-5D1A-413D-8319-81DA479AB0D7&displaylang=en


If you found this link broken then go to google.com and search Download .NET Framework 3.5 Service Pack


Note:- In this example i am getting error about version 3.5.0.0 that's why i am installing .Net framework version 3.5. Check your framework version mentioned in error.


So this is the way to get rid of this Could not load file or assembly 'System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. nasty error.
Read more...

Delete multiple rows in a gridview in asp.net using c#

In this asp.net tutorial you will learn how to delete multiple rows in gridview in asp.net using c#. During web development in number of occasions you may get requirement to delete the multiple rows in a gridview. In asp.net it is not difficult. Let's have a look over how to do so.

Delete multiple rows from gridview in asp.net using c#


.aspx


<asp:gridview id="GridView1" runat="server" width="100%">

<Columns>

<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="chkid" runat="server" Text='<%# Bind("User_ID")%>' />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Email" >
<ItemTemplate>
<asp:Label ID="lbl_Email" Text='<%# Bind("User_Email")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label ID="lbl_first_name" Text='<%# Bind("User_First_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Last Name" >
<ItemTemplate>
<asp:Label ID="lbl_last_name" Text='<%# Bind("User_Last_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>

</Columns>

</asp:gridview>


.aspx.cs


protected void Button1_Click(object sender, EventArgs e)
{

foreach(GridViewRow row in GridView1.Rows)
{
                  CheckBox chkbox = (CheckBox)row.FindControl("chkid");
                  string uid = chkbox.Text;
                  if (chkbox.Checked)
                  {


                   con.ConnectionString=strcon;
                   com.Connection=con;
                   con.Open();
                   string query = "Delete from users where User_ID='"+uid+"'";
                   com.CommandText = query;
                   com.ExecuteNonQuery();
                   con.Close();
                  fillgrid();

               }
}

}



Now let's first understand the code written in .aspx page.

I just put a gridview control in my aspx page just like the tutorial I have written to Displaying data from database in gridview. The only difference is that in very first <asp:TemplateField> I put a checkbox inside its <ItemTemplate> and give the checkbox User_ID as a Text which is the primary key in my users table. It is very much compulsory to provide unique/primary key to the checkbox so that deletion of multiple rows can be achieved without any problem. And then in the end, just below the gridview control I put the asp:button and using its server side onClick event I have written the code to delete the multiple rows in a gridview.

In above code snippet I am using the foreach loop which simply goes to each row of the gridview and then check whether the checkbox of that row is checked or not, if the checkbox of that row is checked then it will simply delete that row.

In the last I made call to the fillgrid() function to display the remaining records in gridview after deletion process. As I have already written this fillgrid() function in my previous tutorial Displaying data from database in gridview that's why in this post i avoid extra code. So That's it

Happy Coding!!!

Read more...

Redirection after some time delay in asp.net using c#

In this tutorial you will learn how to redirect user after some time delay in asp.net using c#. Normally people use redirection process after showing some message to their users, for example a message can be "Thank you for visiting our website" or after getting feedback from user you can display a message "Thank you for providing us your valuable feedback" and then redirect user to another page. It's just up to you in which scenario you want to use it. In asp.net normally we have three options to redirect user after some time delay.
1) Use javascript code in your c# code
2) Use Meta tag in your c# code
3) Use Response.AddHeader().

Redirection after some time delay in asp.net using c#

Let's have a look over these methods

1) Use javascript function in your c# code.
I have already written a post related how to write and execute the javascript code from c# code behind. You just have to put your javascript code in c# code behind and that's it. Let’s have a look over example given below

Yourpage1.aspx

<asp:Button ID="btnRedirect" runat="server" Text="Redirect" OnClick="btnRedirect_Click" />
In my .aspx page I have an asp:button control and using it’s onClick event I am redirecting user.

Yourpage1.aspx.cs
protected void btnRedirect_Click(object sender, EventArgs e)
{
string redirectionScript = "<script language='javascript'>" +
"function Delayer(){"+
"setTimeout('Redirection()', 5000);"+
"}"+
"function Redirection(){" +
"window.location = 'yourpage2.aspx';" +
"}" +
"Delayer()" +
"</script>";
Page.RegisterStartupScript("Startup", redirectionScript);
} 
It will redirect user to yourpage2.aspx after 5 seconds
 

2) Use meta tag in your c# code
I have already written a post for Implementing meta tags with master page in asp.net using c#. Let’s have a look over how to redirect user after some time delay by using meta tag in c# code behind.
protected void btnRedirect_Click(object sender, EventArgs e)
{
HtmlMeta equiv = new HtmlMeta();
equiv.HttpEquiv = "refresh";
equiv.Content = "5; url= yourpage2.aspx";
Header.Controls.Add(equiv);
}
OR
protected void btnRedirect_Click(object sender, EventArgs e)
{
Page.Header.Controls.Add(new LiteralControl("<meta http-equiv='refresh' content='5; url=yourpage2.aspx'/>"));
} 

It will redirect user to yourpage2.aspx after 5 seconds.

3) Use Response.AddHeader()


Use c# built-in method AddHeader() of Response Class to redirect user after some time delay. Let's have a look over how to do so

protected void btnRedirect_Click(object sender, EventArgs e)
{
Response.AddHeader("REFRESH", "5;URL=yourpage2.aspx");
}

It will redirect user to yourpage2.aspx after 5 seconds.


This AddHeader()
function takes two Parameters. First one is HeaderName which is a string indicating the name of the new header. The second parameter is HeaderValue which is a string that indicates the initial value of the new header. It doesn't return any value.

So that's it.



Happy Coding
Read more...

How to restrict special characters being entered in textbox using javascript?

In this tutorial you will learn how to restrict the special characters being entered in textbox. Sometimes you want to restrict some special characters being entered in textbox of form to prevent the sql injection that can harm your database very badly. So Let's have a look over how to do so.


How to restrict special characters being entered in textbox using javascript?


<script language="javascript" type="text/javascript">
function check(e) {
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event)
{
keynum = e.keyCode
}
// For Netscape/Firefox/Opera
else if (e.which)
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
//List of special characters you want to restrict
if (keychar == "'" || keychar == "`")
{

return false;
}
else {
return true;
}
}
</script>

Now let's have a look over how to call this special characters restriction function in a textbox.

For aspx server side textbox control
<asp:TextBox ID="txtName" runat="server" onkeypress="return check(event)" ></asp:TextBox>

For html textbox control
<input type="text" name="txtName" id="txtName" onkeypress="return check(event)">

You can restrict any other character or number other than special characters too, just by simply add it in the list of characters to be restricted such as for example I want to restrict the character a too then the code will be
if (keychar == "'" || keychar == "`" || keychar == "a")

So this is the way to handle and restrict the special characters being entered in textbox using javascript.

Keep Coding…

Read more...