Hi, I am trying to update db using session, but unable to do it..below is my code..redirect is working fine but I have added update on second page and to update using session but its not working ...
string t = Session["s"].ToString();
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "Update Labtest_Table set Name='" + tbName.Text + "', Desgn='" + tbDesg.Text + "', Cata='" + tbCat.Text + "' where ID = @ID2";
cmd.Parameters.AddWithValue("@ID2",t);
int row = cmd.ExecuteNonQuery();
cn.Close();
tbName.Text = "";
tbDesg.Text = "";
tbCat.Text = "";
plz help....
Loading
Sanjeeb LenkaPosted Sep 4, 2013, 12:31 AM
put you pageload code within Is not post back .like below and check
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int t = Convert.ToInt32(Session["s"]);
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "Select * From Labtest_Table Where ID=@ID";
cmd.Parameters.AddWithValue("@ID", t);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
tbName.Text = dr["Name"].ToString();
tbDesg.Text = dr["Desgn"].ToString();
tbCat.Text = dr["Cata"].ToString();
}
cn.Close();
}
}
Sanjeeb LenkaPosted Sep 4, 2013, 3:07 AM
you can do it in rowcommand event of gridview. if any prblm reply
Aman JenPosted Sep 4, 2013, 12:41 AM
plz help...
Aman JenPosted Sep 4, 2013, 12:38 AM
Aman JenPosted Sep 4, 2013, 12:22 AM
Sanjeeb LenkaPosted Sep 4, 2013, 12:15 AM
is your query updating data in database on update button click.
Aman JenPosted Sep 4, 2013, 12:02 AM
code: FirstPage
public partial class SessionNQuerystring : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["myconstr"].ConnectionString;
public SqlConnection cn;
public SessionNQuerystring()
{
cn = new SqlConnection(constr);
}
public SqlCommand cmd = new SqlCommand();
protected void Button1_Click(object sender, EventArgs e)
{
StoreSessionInfo();
}
public void StoreSessionInfo()
{
int s = Convert.ToInt32(TextBox1.Text);
Session["s"] = s;
Response.Redirect("Details.aspx");
}
---------------------------------------------------
code on second page
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;
using System.Configuration;
namespace AddressNTelephone
{
public partial class Details : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["myconstr"].ConnectionString;
public SqlConnection cn;
public Details()
{
cn = new SqlConnection(constr);
}
public SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
int t = Convert.ToInt32(Session["s"]);
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "Select * From Labtest_Table Where ID=@ID";
cmd.Parameters.AddWithValue("@ID",t);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
tbName.Text=dr["Name"].ToString();
tbDesg.Text=dr["Desgn"].ToString();
tbCat.Text=dr["Cata"].ToString();
}
cn.Close();
}
protected void Button5_Click(object sender, EventArgs e)
{
int t = Convert.ToInt32(Session["s"]);
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "Update Labtest_Table set Name='" + tbName.Text + "', Desgn='" + tbDesg.Text + "', Cata='" + tbCat.Text + "' where ID = @ID2";
cmd.Parameters.AddWithValue("@ID2",t);
int row = cmd.ExecuteNonQuery();
cn.Close();
tbName.Text = "";
tbDesg.Text = "";
tbCat.Text = "";
}
}
}
plz help..
Sanjeeb LenkaPosted Sep 3, 2013, 2:25 PM
just put a debug point and check are you getting value from session to t.