Thursday, 28 March 2013

How To Get Newly Inserted Record In Sql Server DataBase Using ADO.Net In Asp.Net

How To Get Newly Inserted Record In Sql Server DataBase Using ADO.Net In Asp.Net


Now on page one add an asp.net button control and put the code on it`s click event.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:button runat="server" text="Clic To Pass Request Quesry String Value"
id="btnclick" onclick="btnclick_Click" />
</div>
</form>
</body>
</html>

C#:
protected void btnclick_Click(object sender, EventArgs e)
{
Response.Redirect("Page2.aspx?value1=100000&value2=20000");
}

In aboce c# code i have passed two value value1 and value2 as a request querystring and i have assigh value to it.

Now come to your page2.aspx. In this page add two lable . This label is used for displaying the request quesrystring value.

<body>
<form id="form1" runat="server">
<div>
<h2>Result of request querystring</h2><br />
Value 1 :<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
Value 3 : <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</div>
</form>
</body>

Now open .cs page for adding hte coce.

protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["value1"] != null)
{
Label1.Text = Request.QueryString["value1"].ToString();
}
if (Request.QueryString["value2"] != null)
{
Label2.Text = Request.QueryString["value2"].ToString();
}
}

In above code i have checked that whether the querystring is null or not if not then assign the value to label. If we will not put any check and some how name got changed on that case we will get object refrance not set to an instance an object.

Now run the code you will get below screen

Now click on button. But before that put a creak point on page load of page2.cs.

In above it`s showing the value of request querysting .

_______________________________

________________________________

No comments:

Post a Comment