Sunday, December 28, 2008

RoleGroups property

RoleGroups property
-------------------------------------------------------------------------------------------------

The RoleGroups property contains the content templates associated with various roles on the Web site. The collection in the RoleGroups property is searched in the order in which templates are defined in the source. The first matching role-group template is displayed to the user. If a user is a member of more than one role, the first role-group template that matches any of the user's roles is used. If more than one template is associated with a single role, only the first defined template will be used.

If a logged-in user does not belong to any role contained in the role-group collection, the site displays the content template specified in the LoggedInTemplate property. Anonymous users are never shown any template contained in the RoleGroups collection.

Download source Code Click the Download Button


asp:LoginView ID="LoginView1" runat="server"
RoleGroups
asp:RoleGroup Roles="admin"
ContentTemplate
Add a new article.br
Review editorial changes.br
View article requests.br
your a logged in here as a Adminbr
ContentTemplate
asp:RoleGroup
asp:RoleGroup Roles="superuser"
ContentTemplate
Add a new article.
Review editorial changes.
View article requests.
your a logged in here as a Superuser.

ContentTemplate
asp:RoleGroup
RoleGroups
LoggedInTemplate
You Are Logged in here as a normal user
LoggedInTemplate
AnonymousTemplate
asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8"
BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.8em" ForeColor="#333333"
TextBoxStyle Font-Size="0.8em"
LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775"
InstructionTextStyle Font-Italic="True" ForeColor="Black"
TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em"
ForeColor="White"
asp:Login
AnonymousTemplate
asp:LoginView



Note : if you need Example code. mail me at rezakawser@yahoo.com

Sunday, November 9, 2008

Passing parameters from one ASP.NET(.aspx) page to another

Passing parameters from one page to another is a very common task in Web development. Granted, its importance and frequency has faded a bit with ASP.NET's inherent preference for postback forms, but regardless, there are still many situations in which you need to pass data from one Web page to another. There are three widely used methods of passing values from one page to another in ASP.NET


1. Using Query String
We usually pass value through query string of the page and then this value is pulled from Request object in another page.


FirstForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);


SecondForm.aspx.cs

TextBox1.Text = Request. QueryString["Parameter"].ToString();

This is the most reliable way when you are passing integer kind of value or other short parameters.

More advance in this method if you are using any special characters in the value while passing it through query string, you must encode the value before passing it to next page. So our code snippet of will be something like this:


FirstForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));


SecondForm.aspx.cs

TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());

2. Passing value through context object
Passing value through context object is another widely used method.



FirstForm.aspx.cs

TextBox1.Text = this.Context.Items["Parameter"].ToString();

SecondForm.aspx.cs

this.Context.Items["Parameter"] = TextBox1.Text;
Server.Transfer("SecondForm.aspx", true);

Note that we are navigating to another page using Server.Transfer instead of Response.Redirect.

Some of us also use Session object to pass values. In that method, value is store in Session object and then later pulled out from Session object in Second page.


3. Posting form to another page instead of PostBack
Third method of passing value by posting page to another form. Here is the example of that:


FirstForm.aspx.cs

private void Page_Load(object sender, System.EventArgs e)
{
buttonSubmit.Attributes.Add("onclick", "return PostPage();");
}

And we create a javascript function to post the form.

SecondForm.aspx.cs

function PostPage()
{
document.Form1.action = "SecondForm.aspx";
document.Form1.method = "POST";
document.Form1.submit();
}
TextBox1.Text = Request.Form["TextBox1"].ToString();


Here we are posting the form to another page instead of itself. You might get viewstate invalid or error in second page using this method. To handle this error is to put EnableViewStateMac=false

4. Another method is by adding PostBackURL property of control for cross page post back

In ASP.NET 2.0, Microsoft has solved this problem by adding PostBackURL property of control for cross page post back. Implementation is a matter of setting one property of control and you are done.



FirstForm.aspx.cs



SecondForm.aspx.cs

TextBox1.Text = Request.Form["TextBox1"].ToString();


In above example, we are assigning PostBackUrl property of the button we can determine the page to which it will post instead of itself. In next page, we can access all controls of the previous page using Request object.

You can also use PreviousPage class to access controls of previous page instead of using classic Request object.



SecondForm.aspx

TextBox textBoxTemp = (TextBox) PreviousPage.FindControl("TextBox1");
TextBox1.Text = textBoxTemp.Text;


As you have noticed, this is also a simple and clean implementation of passing value between pages.


Passing values between pages is another common task accomplishes in web based development. As we have discussed many of mechanisms above, I prefer and recommend to use Query String then other methods for its clean and simple implementation as long as your parameter doesnt have security concern.

Friday, September 12, 2008

DataBase Connection MS SQL 2005 with ASP.NET C#

DataBase Connection MS SQL 2005 with ASP.NET C#




Source Code Download.Simply Click Download.



Note: If you want Documents of this code then join our Groups
Click to join DotNetBD_VB_CsHARP

Click to join DotNetBD_VB_CsHARP


DataBase Connection MS SQL 2000 with ASP.NET C#

DataBase Connection MS SQL 2000 with ASP.NET C#




Source Code Download.Simply Click Download.



Note: If you want Documents of this code then join our Groups
Click to join DotNetBD_VB_CsHARP

Click to join DotNetBD_VB_CsHARP


DataBase Connection MS Access with ASP.NET C#

DataBase Connection MS Access with ASP.NET C#




Source Code Download.Simply Click Download.


Note: If you want Documents of this code then join our Groups
Click to join DotNetBD_VB_CsHARP

Click to join DotNetBD_VB_CsHARP

----------------------------------------------------------------------------------------------


In this article, we will see how to create a database connection between ASP.NET(C#).We will explore design a website and write server code for this project.

In this articles insert, update, delete and search data from MS access database ASP.NET 2.0 and ASP.NET 3.5 (language C#).we had discussed how to save and retrieve images in the database. In this article, we will build on that concept and use the same technique to display data in a grid view.

We will learn how to insert an data and then display the data on the same page with details view . At the end of this article, you will also learn how to use the database in ASP.NET 2.0 and ASP.NET 3.5 (language C#). If you are unfamiliar with the gridview control, I suggest you to read the gridview control in ASP.NET 3.5 to get an understanding of the same

Let us start off by first creating a sample database and adding a table to it. We will call the database ‘SampleDB’ and the table will be called ‘Info_TBL’. This table will contain some column along with some other columns.




Step 1: Create a new ASP.NET website. In the code-behind, add the following namespace

C#




using System.Data.OleDb;

Step 2: Drag and drop table, label, textbox and button control as much we need. Also drag drop some validation control as we need. The source would look similar to the following:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


WelCome || Sample Database connetion Between ASP.NET C# and MS Access Datab


And Snap shot of this code is




Connection string is

String ConnectionStringSampleDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\DatabaseDB.mdb;Persist Security Info=True";

Step 3: In the button click event, add the following code:

C#



protected void btnInsert_Click(object sender, EventArgs e)
{

OleDbConnection cn = new OleDbConnection(ConnectionStringSampleDB);//create connection
cn.Open();//Open connection
string strSave = "insert into Info_TBL Values(";
strSave = strSave + "'" + txtName.Text + "'";
strSave = strSave + ",'" + txtEmail.Text + "'";
strSave = strSave + ",'" + txtPhone.Text + "'";
strSave = strSave + ",'" + txtAdd.Text + "'";
strSave = strSave + ",'" + txtJob.Text + "'";
strSave = strSave + ",'" + txtComments.Text + "')";

OleDbCommand cmd = new OleDbCommand(strSave, cn);//create command
cmd.ExecuteNonQuery();//Finally execute
cn.Close();//connection close

lblmgs.Text = "Succesfully Insert Data";
LoadData();
Clear_Data();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{

OleDbConnection cn = new OleDbConnection(ConnectionStringSampleDB);
cn.Open();//Open connection

string strUpdate = "update Info_TBL set Email = '" + txtEmail.Text + "',Phone ='" + txtPhone.Text + " ',Address ='" + txtAdd.Text + "',Job ='" + txtJob.Text + "'where Name ='" + txtName.Text + "'";
OleDbCommand sqlCmd = new OleDbCommand(strUpdate, cn);
sqlCmd.ExecuteNonQuery();

lblmgs.Text = "Update successfully";
cn.Close();
LoadData();

}
protected void btnDelete_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
lblmgs.Text = "You need to Put Name!!!";
}
else
{

OleDbConnection cn = new OleDbConnection(ConnectionStringSampleDB);
cn.Open();//Open connection

string strDel = "Delete from Info_TBL where Name='" + txtName.Text + "'";
OleDbCommand oleCmd = new OleDbCommand(strDel, cn);
oleCmd.ExecuteNonQuery();

lblmgs.Text = "Deleted successfully";
cn.Close();
LoadData();
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{

OleDbConnection cn = new OleDbConnection(ConnectionStringSampleDB);
cn.Open();//Open connection
string strserch = "Select * from Info_TBL Where Name='" + txtSearch.Text + "'";

OleDbDataAdapter da = new OleDbDataAdapter(strserch, cn);// create data adapter
DataTable dt = new DataTable();//declare data table
DataSet ds = new DataSet();// declare a data set
da.Fill(ds, "Info_TBL");//
dt = ds.Tables["Info_TBL"];

if (dt.Rows.Count > 0)//search data is present or not
{
txtName.Text = dt.Rows[0]["Name"].ToString();
txtEmail.Text = dt.Rows[0]["Email"].ToString();
txtPhone.Text = dt.Rows[0]["Phone"].ToString();
txtAdd.Text = dt.Rows[0]["Address"].ToString();
txtJob.Text = dt.Rows[0]["Job"].ToString();
txtComments.Text = dt.Rows[0]["Comments"].ToString();

}
else
{
lblmgs.Text = "Data Not Found";
}
cn.Close();
LoadData();

}

In the code above, we are creating a connetion. In this connection four operations are performed.

Step 4: In order to display the data Gridview on the page, first we will add a Gridview then we will create another connections and query String then we will bind the whole data with Gridview.






void LoadData()

{

OleDbConnection cn = new OleDbConnection(ConnectionStringSampleDB);

cn.Open();//Open connection

string strserch = "Select * from Info_TBL";

OleDbDataAdapter da = new OleDbDataAdapter(strserch, cn);

DataSet ds = new DataSet();

da.Fill(ds, "Info_TBL");// load data into adadpter

GridView1.DataSource = ds;

GridView1.DataBind();// finally bind in datagrid

}



void Clear_Data()

{

txtName.Text = string.Empty;

txtEmail.Text = string.Empty;

txtPhone.Text = string.Empty;

txtJob.Text = string.Empty;

txtAdd.Text = string.Empty;

txtComments.Text = string.Empty;

}

Step 5: here we write a Clear_data




All then connections techniques are explain if u want to download source code of this Connection please Go to this link. Source Code