Click here to Skip to main content
16,019,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to retrieve id from database table by selecting a particular row in data grid view and display in next page?
Posted
Comments
Member 9581488 6-Dec-12 9:22am    
How do u select a row? What event are you using?
Amalachris 6-Dec-12 9:23am    
using hyperlink..

write this code in 1st page
DEFAULT.ASP

XML
<div>
    <a href="ShowDetails.aspx?autoid=5" title="Show records where AutoId is 5">Show records where AutoId is 5 </a>
    <p><asp:HyperLink ID="hyper1" runat="server" Text="Show Record where AutoID is 8"> </asp:HyperLink> </p>
</div>


DEFAULT.ASPX.CS CODE BEHIND

C#
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
     hyper1.NavigateUrl = "ShowDetails.aspx?autoid=8&com=show";
   }
}



SHOWDETAILS.ASPX

XML
<div>
    <asp:DetailsView ID="DetailsView1" runat="server" EnableViewState="false" />
</div>




SHOWDETAILS.ASPX.CS CODE BEHIND

rotected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    if (!string.IsNullOrWhiteSpace(Request.QueryString["autoid"]))
    {
      int autoId = 0;
      int.TryParse(Request.QueryString["autoid"], out autoId);
      if (!autoId.Equals(0))
      {
        GetData(autoId);
      }
    }
    // to get the other querystring do the same
    string command = Request.QueryString["com"];
  }
}
private void GetData(int autoId)
{
   DataTable table = new DataTable();
   // get the connection
   using (SqlConnection conn = new SqlConnection(_connStr))
   {
      // write the sql statement to execute
      string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail WHERE AutoId = @AutoId ORDER By AutoId";
      // instantiate the command object to fire
      using (SqlCommand cmd = new SqlCommand(sql, conn))
      {
         // get the adapter object and attach the command object to it
         using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
         {
           SqlParameter prm = new SqlParameter("@AutoId", SqlDbType.Int);
           prm.Value = autoId;
           cmd.Parameters.Add(prm);
           // fire Fill method to fetch the data and fill into DataTable
           ad.Fill(table);
         }
      }
  }
  // specify the data source for the GridView
  DetailsView1.DataSource = table;
  // bind the data now
  DetailsView1.DataBind();
}
 
Share this answer
 
You can use request query string or session variable to store id of selected row.
 
Share this answer
 
Comments
Amalachris 6-Dec-12 9:34am    
ya i got it.. thanks :)
C#
if(!String.IsNullOrEmpty(Request.QueryString("TextBoxValue"))

{

  TextBox1.Text = Request.QueryString("TextBoxValue");

}



using that value to store in database.....
 
Share this answer
 

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