Here i am passing the TextBox value from one page to another page. on second page i have created one property for retrieving QueryString values
First.aspx:
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="txtId" />
<asp:Button Text="Get Information based on EmployeeID" OnClick="GetInformation" runat="server" />
</div>
</form>
C#:
protected void GetInformation(object sender, EventArgs e)
{
Response.Redirect("Grid.aspx?EmployeeId=" + this.txtId.Text.Trim());
}
VB.Net:
Protected Sub GetInformation(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("Grid.aspx?EmployeeId=" & Me.txtId.Text.Trim())
End Sub
Second.aspx:
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
</Columns>
</asp:GridView>
</div>
</form>
C#:
private int EmployeeId
{
get
{
return (Request.QueryString["EmployeeId"] == null ? 0 : int.Parse(Request.QueryString["EmployeeId"]));
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
PopulateEmployees();
}
}
public void PopulateEmployees()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string sqlStatment = "SELECT EmployeeID,FirstName,LastName FROM Employees WHERE EmployeeID = @EmployeeId";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@EmployeeId", this.EmployeeId);
DataSet ds = new DataSet();
da.Fill(ds);
this.gvEmployees.DataSource = ds;
this.gvEmployees.DataBind();
}
}
}
}
VB.Net:
Private ReadOnly Property EmployeeId As Integer
Get
Return (If(Request.QueryString("EmployeeId") Is Nothing, 0, Integer.Parse(Request.QueryString("EmployeeId"))))
End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
PopulateEmployees()
End If
End Sub
Public Sub PopulateEmployees()
Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Dim sqlStatment As String = "SELECT EmployeeID,FirstName,LastName FROM Employees WHERE EmployeeID = @EmployeeId"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(sqlStatment, con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
cmd.Parameters.AddWithValue("@EmployeeId", Me.EmployeeId)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
Me.gvEmployees.DataSource = ds
Me.gvEmployees.DataBind()
End Using
End Using
End Using
End Sub
Thank You.