Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi iam new to asp. i used the post method to pass value from one page to the another.
my first page de.aspx and my second page d2.aspx. from the first page i passed the value to the second page and displayed it in the textbox of the second page . from there iam not able to enter it into the database.

de.aspx


C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    string s = GridView1.SelectedRow.Cells[1].Text;
    String d = GridView1.SelectedRow.Cells[2].Text;
    Response.Redirect("Data1.aspx?code="+s+"&name="+d);
        
}

second page d2.aspx




C#
protected void Button1_Click(object sender, EventArgs e)
{
   try
   {
       TextBox3.Text = TextBox1.Text;
       TextBox4.Text = TextBox2.Text;        
        SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=me;User ID=sa;Password=windows");
        SqlCommand c = new SqlCommand("INSERT INTO [deptab](depcode,depname)values(@code,@name)", con);
        con.Open();
        c.Parameters.AddWithValue("@code", TextBox3.Text);
        c.Parameters.AddWithValue("@name", TextBox4.Text);
        c.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception el)
    {
        el.GetBaseException();
    }   
}
Posted
Updated 23-Oct-11 18:55pm
v3
Comments
ashok_89 24-Oct-11 8:51am    
guys the problem i noted is,in the page D2 the previous pages selected value is being displayed in the textboxes, though i changed the values in the textboxes .its changing in the textboxes but actually when i retreive it on to the variable
the previous value only displaying not the "the value that i edited in the textbox". the edited value only i need to insert into the DB and adding to that i used the primary key for the dcode value so its not inserting into the DB.

From de.aspx, you need to pass values to d2.aspx
C#
Response.Redirect("d2.aspx?code="+s+"&name="+d);


In d2.aspx, you need to get those values in Page_Load event
C#
if (!Page.IsPostBack)
{
  TextBox2.Text = Page.Request.QueryString["name"];
  TextBox1.Text = Page.Request.QueryString["code"];
}


and then on button click you can save it with your code.
 
Share this answer
 
v2
Comments
ashok_89 24-Oct-11 8:52am    
guys the problem i noted is,in the page D2 the previous pages selected value is being displayed in the textboxes, though i changed the values in the textboxes .its changing in the textboxes but actually when i retreive it on to the variable
the previous value only displaying not the "the value that i edited in the textbox". the edited value only i need to insert into the DB and adding to that i used the primary key for the dcode value so its not inserting into the DB.
Prerak Patel 24-Oct-11 23:41pm    
Ah, so you want the edited value! Put an if condition to check postback in load event and you are done. Modified the answer.
ashok_89 25-Oct-11 0:32am    
awesome sir its working fine thanks a lot. newly joined the job and dont know anything regarding dotnet how can i start learning but iam well versed in oops concepts
Prerak Patel 25-Oct-11 2:08am    
You are welcome. I think you just google or refer to a good book to learn the things.
On manipulating the common data between two pages, you can use the global data container (i.e. some thing like global variable). Itz the easy way to play around the passed arguments.
 
Share this answer
 
Comments
ashok_89 24-Oct-11 8:52am    
guys the problem i noted is,in the page D2 the previous pages selected value is being displayed in the textboxes, though i changed the values in the textboxes .its changing in the textboxes but actually when i retreive it on to the variable
the previous value only displaying not the "the value that i edited in the textbox". the edited value only i need to insert into the DB and adding to that i used the primary key for the dcode value so its not inserting into the DB.
Dear Friend,

Your Passing the Values From One Page to Another Page But your not Binding the Values to TextBox that is Controls.

So,in page Load

TextBox2.Text  = Request.QueryString["name"].ToString();

  TextBox1.Text = Request.QueryString["Code"].ToString();

Then you can insert the values in Your DB.


Regards,

Anilkumar.D
 
Share this answer
 
v2
Comments
ashok_89 24-Oct-11 8:52am    
guys the problem i noted is,in the page D2 the previous pages selected value is being displayed in the textboxes, though i changed the values in the textboxes .its changing in the textboxes but actually when i retreive it on to the variable
the previous value only displaying not the "the value that i edited in the textbox". the edited value only i need to insert into the DB and adding to that i used the primary key for the dcode value so its not inserting into the DB.
on d2.aspx ,You have to fetch values from querystring.

Like this,

TextBox1.Text = Page.Request.QueryString["code"];
TextBox2.Text = Page.Request.QueryString["name"];


Check the values using breakpoints
 
Share this answer
 
v2
Comments
ashok_89 24-Oct-11 8:52am    
guys the problem i noted is,in the page D2 the previous pages selected value is being displayed in the textboxes, though i changed the values in the textboxes .its changing in the textboxes but actually when i retreive it on to the variable
the previous value only displaying not the "the value that i edited in the textbox". the edited value only i need to insert into the DB and adding to that i used the primary key for the dcode value so its not inserting into the DB.
C#
//de.aspx

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
            GridViewRow row = GridView1.SelectedRow;
            string s = GridView1.SelectedRow.Cells[1].Text;
            string d = GridView1.SelectedRow.Cells[2].Text;
            Response.Redirect("Data1.aspx?code="+s+"&name="+d); //HERE YOU ARE PASSING THE VALUE TO <code>DATA1.ASPX</code> PAGE NOT <code>d2.aspx</code> PAGE SO HOW YOU GET YOUR VALUE TO d2.aspx..
            
      }


FOR GETTING VALUE TO d2.aspx you need to pass value to that page like this..

C#
Response.Redirect("d2.aspx?code"+s+"&name="+d);


AND ON THE d2.aspx Page you can get your value like this..


C#
//INSIDE YOUR PAGE LOAD EVENT OF YOUR d2.aspx PAGE YOU NEED TO ASSIGN THE VALUE TO TEXTBOXES LIKE THIS
        TextBox1.Text = Request.QueryString["code"].toString();
        TextBox2.Text = Request.QueryString["name"].toString();

C#
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
                        
        TextBox3.Text = TextBox1.Text;
        TextBox4.Text = TextBox2.Text;            
        SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=me;User ID=sa;Password=windows");
        SqlCommand c = new SqlCommand("INSERT INTO [deptab](depcode,depname)values(@code,@name)", con);
        con.Open();
        c.Parameters.AddWithValue("@code", TextBox3.Text);
        c.Parameters.AddWithValue("@name", TextBox4.Text);
        c.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception el)
    {
    el.GetBaseException();
    }
    
    }
 
Share this answer
 
v2
Comments
ashok_89 24-Oct-11 8:52am    
guys the problem i noted is,in the page D2 the previous pages selected value is being displayed in the textboxes, though i changed the values in the textboxes .its changing in the textboxes but actually when i retreive it on to the variable
the previous value only displaying not the "the value that i edited in the textbox". the edited value only i need to insert into the DB and adding to that i used the primary key for the dcode value so its not inserting into the DB.

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