Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can we do this without using querystring,,,please provide me code...


editpage.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace WebApplication1
{
    public partial class editpage : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(@"Data Source=SRAVI-PC\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDetails();
            }
        }
        private void BindDetails()
        {
            lblid.Text = Request.QueryString["Id"];
//            txtid.Text = Request.QueryString["Id"];
            txtfname.Text = Request.QueryString["FirstName"];
            txtlname.Text = Request.QueryString["LastName"];
        }
        
        protected void btnsave_Click(object sender, EventArgs e)
        {
            int id1 = Convert.ToInt32(lblid.Text);
//            int id = Convert.ToInt32(txtid.Text);
            string fna = txtfname.Text;
            string lna = txtlname.Text;
            string cmd = "update namestb set FirstName='" + fna + "',LastName='"+lna+"'where Id="+id1;
            SqlCommand command = new SqlCommand(cmd, conn);
            conn.Open();
            command.ExecuteNonQuery();
            conn.Close();
            Response.Redirect("displaypage.aspx");
        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("displaypage.aspx");
        }
    }
}


display.aspx.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class displaypage : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(@"Data Source=SRAVI-PC\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            getdata();
        }
        public void getdata()
        {
            string cmd = "select * from namestb";
            SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

        protected void edit(object sender,CommandEventArgs z)
        {
            if (z.CommandName == "edit")
            {
                int id = Convert.ToInt32(z.CommandArgument);
                SqlDataAdapter da = new SqlDataAdapter("select * from namestb where Id=" + id, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "namestb");
                DataTable dt = new DataTable();
                dt = ds.Tables[0];
                int Id = Convert.ToInt16(dt.Rows[0]["Id"]);
                string FirstName = Convert.ToString(dt.Rows[0]["FirstName"]);
                string LastName = Convert.ToString(dt.Rows[0]["LastName"]);
                Response.Redirect("editpage.aspx?Id=" + Id + "&FirstName=" + FirstName + "&LastName=" + LastName);

            }
        }
    }
}
Posted

1 solution

Yes - transfer the details via either Cookies or the Session instead.

But please, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
Prasad_Kulkarni 13-Jun-13 9:16am    
5'ed!
sravisravi 14-Jun-13 1:35am    
thank you sir,
but,
actually my task is "editing the gridview, when we click on edit that row should be open in editable mode in an another page(using above code)"
but here I want to edit that row in same page only,when we click on edit on gridview,that gridview should be hide and that specific fields open in editable mode,
how can we do this?
could you clarify me sir...,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900