ASP.NET:GENERATING DYNAMIC URL AND FETCHING THE VALUE FROM URL

Hello,

Here i have generated url based on the user id(primary key) and the id is retreived using Querystring Function.You can use it incase if you need to generate the link dynamically for different users.

Login.cs:

/*Write this code in a buttonclick function*/

SqlCommand cmd = new SqlCommand("select * from Register where email= @email and password=@password", con);
cmd.Parameters.AddWithValue("email", email.Text);
cmd.Parameters.AddWithValue("password", password.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (dt.Rows.Count > 0)
{
dr = dt.Rows[0];
Session["email"] = email.Text;
Session["id"]= Convert.ToString(dr[0]);    //This id will be used to generate the url
Response.Redirect("Home.aspx");
Session.RemoveAll();
}
else
{
lblMessage.Text = "Your Card No is Incorrect";
lblMessage.ForeColor = System.Drawing.Color.Red;
}

/*If the login is successful ,it will redirect the code to the page “Home.aspx”.

Home.aspx:

/*Write this code in a page load function.It will display the generated url in a textbox*/

string id = Session["id"] as string;
if (Session["id"] != null)
{
//Retrieving UserName from Session
link.Text="Test.aspx?id="+Session["id"];//Url generated with id.If the id is 1 for example,the url will be "Test.aspx?id=1"
}

/*Now Fetch the id value from the address Bar.*/

Test.cs:

/*Write this code in  page load*/

string id = Request.QueryString["id"];  //Fetching id from URL
tb1.Text = "welcome" + id;

 

Thanq!!

Leave a Reply

Your email address will not be published. Required fields are marked *