prophyt
asked on
How to make FormView1.ChangeMode(FormViewMode.ReadOnly) display the recently entered data?
I have a basic formview page with two text box... which I am using OnCommand via a linkbutton to insert data. So for the data get entered correctly into the database. Then I use the FormView1.ChangeMode(FormV iewMode.Re adOnly); to display the recently entered entry. The problem I am having is that is does not show the most recently enterd data but shows the first record in the database... how I get it to show the most recenly entered data.
protected void InsertButton_Command(objec t sender, CommandEventArgs e)
{
if (e.CommandName == "NewInsert")
{
TextBox theFirst = (TextBox)FormView1.FindCon trol("Text Box1");
if (theFirst == null) { theFirst.Text = ""; }
TextBox theSecond = (TextBox)FormView1.FindCon trol("Text Box2");
if (theSecond == null) { theSecond.Text = ""; }
DateTime entryDate = DateTime.Now;
using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CON NECTION))
{
string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond)";
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
objCmd.Parameters.Add(new SqlParameter("@theFirst", theFirst.Text));
objCmd.Parameters.Add(new SqlParameter("@project_tit le", theSecond.Text));
SqlDataReader dataReader = objCmd.ExecuteReader();
objCmd.Connection.Close();
FormView1.ChangeMode(FormV iewMode.Re adOnly);
FormView1.DataBind();
}
}
}
protected void InsertButton_Command(objec
{
if (e.CommandName == "NewInsert")
{
TextBox theFirst = (TextBox)FormView1.FindCon
if (theFirst == null) { theFirst.Text = ""; }
TextBox theSecond = (TextBox)FormView1.FindCon
if (theSecond == null) { theSecond.Text = ""; }
DateTime entryDate = DateTime.Now;
using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CON
{
string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond)";
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
objCmd.Parameters.Add(new SqlParameter("@theFirst", theFirst.Text));
objCmd.Parameters.Add(new SqlParameter("@project_tit
SqlDataReader dataReader = objCmd.ExecuteReader();
objCmd.Connection.Close();
FormView1.ChangeMode(FormV
FormView1.DataBind();
}
}
}
You have to perform a "SELECT SCOPE_IDENTITY()" before you close the connection. This will give you the ID of the last inserted record. Then you update the parameter for you datasource to this ID and databind.
Cleaned up your code a little.
int id = 0;
using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CONNECTION))
{
string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond)";
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
objCmd.Parameters.Add(new SqlParameter("@theFirst", theFirst.Text));
objCmd.Parameters.Add(new SqlParameter("@project_title", theSecond.Text));
objCmd.ExecuteNonQuery();
sqlString = "SELECT SCOPE_IDENTITY() as ID";
objCmd = new SqlCommand(sqlString, MyConnection);
SqlDataReader dataReader = objCmd.ExecuteReader();
if(dataReader.Read())
{
id = Convert.ToInt32(dataReader["ID"]);
}
objCmd.Connection.Close();
//Set parameter ID here
FormView1.ChangeMode(FormViewMode.ReadOnly);
FormView1.DataBind();
}
ASKER
I have made the following modification but I keep getting an "Must declare the variable '@id'. " error message:
protected void InsertButton_Command(objec t sender, CommandEventArgs e)
{
if (e.CommandName == "NewInsert")
{
TextBox theFirst = (TextBox)FormView1.FindCon trol("Text Box1");
if (theFirst == null) { theFirst.Text = ""; }
TextBox theSecond = (TextBox)FormView1.FindCon trol("Text Box2");
if (theSecond == null) { theSecond.Text = ""; }
DateTime entryDate = DateTime.Now;
using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CON NECTION))
{
int @id = 0;
string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond); SELECT @id = SCOPE_IDENTITY()";
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
objCmd.Parameters.Add(new SqlParameter("@theFirst", theFirst.Text));
objCmd.Parameters.Add(new SqlParameter("@project_tit le", theSecond.Text));
SqlDataReader dataReader = objCmd.ExecuteReader();
objCmd.Connection.Close();
SqlDataSource1.SelectComma nd = "SELECT [id], [theFirst, [theSecond] FROM [n_tblEvaluation] WHERE [id] = @id";
FormView1.ChangeMode(FormV iewMode.Re adOnly);
FormView1.DataBind();
}
}
}
protected void InsertButton_Command(objec
{
if (e.CommandName == "NewInsert")
{
TextBox theFirst = (TextBox)FormView1.FindCon
if (theFirst == null) { theFirst.Text = ""; }
TextBox theSecond = (TextBox)FormView1.FindCon
if (theSecond == null) { theSecond.Text = ""; }
DateTime entryDate = DateTime.Now;
using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CON
{
int @id = 0;
string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond); SELECT @id = SCOPE_IDENTITY()";
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
objCmd.Parameters.Add(new SqlParameter("@theFirst", theFirst.Text));
objCmd.Parameters.Add(new SqlParameter("@project_tit
SqlDataReader dataReader = objCmd.ExecuteReader();
objCmd.Connection.Close();
SqlDataSource1.SelectComma
FormView1.ChangeMode(FormV
FormView1.DataBind();
}
}
}
ASKER
sorry I just noticed your earlier post... I hadn't refresh...
But I tried as slightly modify version of your suggestion but it seem like the "ID" keep returning a null value... but the data was successfully entered into the database.
protected void InsertButton_Command(objec t sender, CommandEventArgs e)
{
if (e.CommandName == "NewInsert")
{
TextBox theFirst = (TextBox)FormView1.FindCon trol("Text Box1");
if (theFirst == null) { theFirst.Text = ""; }
TextBox theSecond = (TextBox)FormView1.FindCon trol("Text Box2");
if (theSecond == null) { theSecond.Text = ""; }
DateTime entryDate = DateTime.Now;
int id = 0;
using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CON NECTION))
{
string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond)";
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
objCmd.Parameters.Add(new SqlParameter("@theFirst", theFirst.Text));
objCmd.Parameters.Add(new SqlParameter("@project_tit le", theSecond.Text));
objCmd.ExecuteNonQuery();
sqlString = "SELECT SCOPE_IDENTITY() as ID";
objCmd = new SqlCommand(sqlString, MyConnection);
SqlDataReader dataReader = objCmd.ExecuteReader();
if (dataReader.Read())
{
if (dataReader["ID"] != DBNull.Value)
{
id = Convert.ToInt32(dataReader ["ID"]);
}
else
{
Response.Write("value is null");
}
}
objCmd.Connection.Close();
SqlDataSource1.SelectComma nd = "SELECT [id], [theFirst], [theSecond] FROM [n_tblEvaluation] WHERE [id] = '" + id + "'";
FormView1.ChangeMode(FormV iewMode.Re adOnly);
FormView1.DataBind();
}
}
}
But I tried as slightly modify version of your suggestion but it seem like the "ID" keep returning a null value... but the data was successfully entered into the database.
protected void InsertButton_Command(objec
{
if (e.CommandName == "NewInsert")
{
TextBox theFirst = (TextBox)FormView1.FindCon
if (theFirst == null) { theFirst.Text = ""; }
TextBox theSecond = (TextBox)FormView1.FindCon
if (theSecond == null) { theSecond.Text = ""; }
DateTime entryDate = DateTime.Now;
int id = 0;
using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CON
{
string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond)";
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
objCmd.Connection.Open();
objCmd.Parameters.Add(new SqlParameter("@theFirst", theFirst.Text));
objCmd.Parameters.Add(new SqlParameter("@project_tit
objCmd.ExecuteNonQuery();
sqlString = "SELECT SCOPE_IDENTITY() as ID";
objCmd = new SqlCommand(sqlString, MyConnection);
SqlDataReader dataReader = objCmd.ExecuteReader();
if (dataReader.Read())
{
if (dataReader["ID"] != DBNull.Value)
{
id = Convert.ToInt32(dataReader
}
else
{
Response.Write("value is null");
}
}
objCmd.Connection.Close();
SqlDataSource1.SelectComma
FormView1.ChangeMode(FormV
FormView1.DataBind();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for you help....