Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have a form that a user fills out and clicks submit. Once the data is saved and the user is redirected back to the home screen. I am trying to get the data that the user just saved to populate the form when the user goes back to the form they just filled out. Is there a way of doing this?

C#
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con3.Open();

            SqlCommand scmd3 = new SqlCommand("Select INST_ID, FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from Table33 where INST_ID = '" + TextBoxINST_ID.Text + "'", con3);
            SqlDataReader dr3 = scmd3.ExecuteReader();
            if (dr3.Read())

            TextBoxFTUG.Text = dr3["FT_UNDERGR"].ToString();
            TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
            TextBoxTHUGDR.Text = dr3["FTE_UNDERG"].ToString();
            TextBoxTHGDR.Text = dr3["FTE_GRAD"].ToString();
            TextBoxNCCDR.Text = dr3["NON_CREDIT"].ToString();
            TextBoxTCNC.Text = dr3["TOTAL_FTE"].ToString();
            TextBoxTNFUG.Text = dr3["FCFTUHC"].ToString();
            TextBoxTNFG.Text = dr3["FCFTPBHC"].ToString();
            TextBoxTNCPUG.Text = dr3["FCPTUHC"].ToString();
            TextBoxTNCPG.Text = dr3["FCPTPBHC"].ToString();
            TextBoxTNNCC.Text = dr3["NCHC"].ToString();
            con3.Close();
            dr3.Close();
Posted
Updated 11-Jan-14 11:33am
v2

Loads of ways... Probably what you want to do is save the data and restore it in the page load event, but exactly how you do that is up to you.
If you log your users in, then you might want to save the data in your database and restore forum there. Alternatives are cookies (stored on the client, so can be preserved from session to session, provided the user uses the same PC) or in the Session collection (on the server, so it's lost when the browser page closes, or twenty minutes, whichever happens first)

Have a think about exactly what you want to do, and then Google can help you with more details.
 
Share this answer
 
Comments
Computer Wiz99 11-Jan-14 16:47pm    
OriginalGriff, I currently have a code that populates other textboxes on the form with data from the database but when I try to do the same to the other textboxes I get an error. This is my error: "Invalid attempt to read when no data is present.". I do understand why I get this error but, How can I stop the error if the there is no data until the user enters it? When the page loads is when the error comes for the first time.
OriginalGriff 11-Jan-14 17:03pm    
Depends on your code: have you checked the fields you are fetching from the DB?
Computer Wiz99 11-Jan-14 17:31pm    
Yes I have. They are all correct. Here is the code I just uploaded.
OriginalGriff 11-Jan-14 17:46pm    
Um.
Did you check your TextBoxINST_ID.Text?
Because on page load, it's going to be blank... And that won't return anything, or will return the wrong info...
BTW: for your own sake, don't do SQL like that! Never concatenate strings to form an SQL statement, particularly not with a text box content, and very definitely not with a web application! Google "Bobby Tables" and don't assume it's a joke: SQL Injection can easily damage or destroy your database from the other side of the world. And probably from your login screen as well...

Use parameterized queries... Or make sure your SQL server backups are seriously up to date, because you are going to need them...
Add { and } around the code block that should be executed only when data is read. See the example below. The way your code is written would cause the assignment statements starting with TextBoxFTG.Text = dr3["FT_GRAD"].ToString(); to be executed whether there was data or not.

I added an if statement at the start of the code to check to be sure that there is data in TextBoxINST_ID.Text. This will handle the case when TextBoxINST_ID.Text does not yet contain a value. Also, see comment below about using SQLParameter class.

C#
if (TextBoxINST_ID.Text.Trim().Length > 0)
{
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con3.Open();
 
            SqlCommand scmd3 = new SqlCommand("Select INST_ID, FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from Table33 where INST_ID = '" + TextBoxINST_ID.Text + "'", con3);
            SqlDataReader dr3 = scmd3.ExecuteReader();
            if (dr3.Read())
            {
            TextBoxFTUG.Text = dr3["FT_UNDERGR"].ToString();
            TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
            TextBoxTHUGDR.Text = dr3["FTE_UNDERG"].ToString();
            TextBoxTHGDR.Text = dr3["FTE_GRAD"].ToString();
            TextBoxNCCDR.Text = dr3["NON_CREDIT"].ToString();
            TextBoxTCNC.Text = dr3["TOTAL_FTE"].ToString();
            TextBoxTNFUG.Text = dr3["FCFTUHC"].ToString();
            TextBoxTNFG.Text = dr3["FCFTPBHC"].ToString();
            TextBoxTNCPUG.Text = dr3["FCPTUHC"].ToString();
            TextBoxTNCPG.Text = dr3["FCPTPBHC"].ToString();
            TextBoxTNNCC.Text = dr3["NCHC"].ToString();
            }
            con3.Close();
            dr3.Close();
}


Also, it would be a best practice to use a parameterized query rather than '" + TextBoxINST_ID.Text + "'" in you SELECT statement. This prevents SQL Injection Attacks and provides better performance. See Microsoft help file documentation for SQLParameter Class[^]
 
Share this answer
 
v6
Comments
Karthik_Mahalingam 11-Jan-14 21:52pm    
5! excellent..
Computer Wiz99 12-Jan-14 8:57am    
Mike Meinz and KARTHIK Bangalore, Can this work in a if(true) statement when the page loads?
Mike Meinz 12-Jan-14 9:05am    
I don't understand your question.
Computer Wiz99 12-Jan-14 9:17am    
What I am trying to say is that the code that I have can I use it as a if else statement? If there is data in the database and that is true populate the fields, else if false leave blank.
Mike Meinz 12-Jan-14 9:22am    
The code in my solution above does exactly that. An else block is not needed.

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