Insert (Save) value from QueryString using C# in ASP.Net

indradeo
 
on Oct 11, 2021 05:42 AM
Sample_275028.zip
331 Views

how to insert value from QueryString using asp c#

i passed http://localhost:21566/Default.aspx?empid=7221 from index.aspx to Default.aspx.

I want to save empid value in table where table column is lst_updated_by.

protected void Save(object sender, EventArgs e)
{
    Response.Redirect("Default.aspx?empid=" + Session["empid"].ToString().Trim());
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 11, 2021 05:45 AM

Hi indradeo,

Use below code to get the value of QueryString.

string empid = Request.QueryString["empid"];

Use the variable to insert in table.

arjunv
 
on Oct 12, 2021 05:15 AM

Hii Indradeo,

Kindly follow below Sample.

HTML

Page-1

<table>
    <tr>
        <td>Name:</td>
        <td>
            <asp:TextBox runat="server" ID="txtName"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Employee Id:</td>
        <td>
            <asp:TextBox runat="server" ID="txtEmpId"></asp:TextBox></td>
    </tr>
    <tr>
        <td>
            <asp:Button runat="server" ID="btnSend" Text="Send" OnClick="OnSend" /></td>
    </tr>
</table>

Page-2

<table>
    <tr>
        <td>Name:</td>
        <td>
            <asp:Label runat="server" ID="lblName"></asp:Label></td>
    </tr>
    <tr>
        <td>Employee Id:</td>
        <td>
            <asp:Label runat="server" ID="lblEmpId"></asp:Label></td>
    </tr>
    <tr>
        <td>
            <asp:Button runat="server" ID="btnSend" Text="Send" /></td>
    </tr>
</table>

Namespaces

using System.Configuration;
using System.Data.SqlClient;

Code

Page-1

protected void OnSend (object sender, EventArgs e)
{
    Response.Redirect("Page-2.aspx?Name=" + txtName.Text + "&EmployeeId=" + txtEmpId.Text);
}

Page-2

protected void Page_Load(object sender, EventArgs e)
{
    string name = Request.QueryString["Name"];
    string employeeId = Request.QueryString["EmployeeId"];
    lblName.Text = name;
    lblEmpId.Text = employeeId;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Details (Name,Id) VALUES (@Name,@Id)"))
        {
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Id", employeeId);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}