Solved

How to make FormView1.ChangeMode(FormViewMode.ReadOnly) display the recently entered data?

Posted on 2010-08-27
6
683 Views
Last Modified: 2012-05-10
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(FormViewMode.ReadOnly);  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(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "NewInsert")
        {

            TextBox theFirst = (TextBox)FormView1.FindControl("TextBox1");
            if (theFirst == null) { theFirst.Text = ""; }
            TextBox theSecond = (TextBox)FormView1.FindControl("TextBox2");
            if (theSecond == null) { theSecond.Text = ""; }
               
            DateTime entryDate = DateTime.Now;
            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));
                SqlDataReader dataReader = objCmd.ExecuteReader();
                objCmd.Connection.Close();

                FormView1.ChangeMode(FormViewMode.ReadOnly);
                FormView1.DataBind();
              }
        }
    }
0
Comment
Question by:prophyt
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 33541282
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.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 33541311
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();
              }

Open in new window

0
 

Author Comment

by:prophyt
ID: 33541658
I have made the following modification but I keep getting an "Must declare the variable '@id'. "  error message:

    protected void InsertButton_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "NewInsert")
        {

            TextBox theFirst = (TextBox)FormView1.FindControl("TextBox1");
            if (theFirst == null) { theFirst.Text = ""; }
            TextBox theSecond = (TextBox)FormView1.FindControl("TextBox2");
            if (theSecond == null) { theSecond.Text = ""; }
               
            DateTime entryDate = DateTime.Now;
            using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CONNECTION))
            {
            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_title", theSecond.Text));
                SqlDataReader dataReader = objCmd.ExecuteReader();
                objCmd.Connection.Close();

                SqlDataSource1.SelectCommand = "SELECT [id], [theFirst, [theSecond] FROM [n_tblEvaluation] WHERE [id] = @id";
                FormView1.ChangeMode(FormViewMode.ReadOnly);
                FormView1.DataBind();
              }
        }
    }
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:prophyt
ID: 33542084
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(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "NewInsert")
        {

            TextBox theFirst = (TextBox)FormView1.FindControl("TextBox1");
            if (theFirst == null) { theFirst.Text = ""; }
            TextBox theSecond = (TextBox)FormView1.FindControl("TextBox2");
            if (theSecond == null) { theSecond.Text = ""; }
               
            DateTime entryDate = DateTime.Now;
            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())
                {

                    if (dataReader["ID"] != DBNull.Value)
                    {
                        id = Convert.ToInt32(dataReader["ID"]);
                    }
                    else
                    {
                        Response.Write("value is null");
                    }
                }

                objCmd.Connection.Close();


                SqlDataSource1.SelectCommand = "SELECT [id], [theFirst], [theSecond] FROM [n_tblEvaluation] WHERE [id] = '" + id + "'";
                FormView1.ChangeMode(FormViewMode.ReadOnly);
                FormView1.DataBind();


              }
        }
    }
0
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 33542400
You could try the multipart SQL query like this:
 using (SqlConnection MyConnection = new SqlConnection(myGLOBAL_CONNECTION))

            {



                string sqlString = "INSERT INTO [n_tblEvaluation] ([theFirst], [theSecond) VALUES (@theFirst, @theSecond); SELECT SCOPE_IDENTITY() AS ID;";

                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));

                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();



                FormView1.ChangeMode(FormViewMode.ReadOnly);

                FormView1.DataBind();

              }

Open in new window

0
 

Author Comment

by:prophyt
ID: 33543634
Thanks for you help....
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Editing XSL files, 2 21
C# Application Local DB Connection String 23 61
Chat Room 1 30
Problem to page 4 27
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now