I want to retrieve data from a form submitted from a sql database using Gridview control. I have 2 Textbox and submit buttton on my profile page, when the button is clicked upon the form is submitted and store in my database and if code runs correctly as it dose already it display my successful page. In my successful page, I place a Gridview control on the page.
My intention here is to get the information displayed anytime the form is submitted with a new value but I kept getting the the first original value that was submitted into the database.
Example, if i entered Jone Fox and click enter, it submit to the database, and again enter Ross Tonbull and submit, it also submit to the database as with serial no 2, but it is the first value i enters that will be displayed on my successful page
How can i get the exact any value enters to display only using my Gridview or Detailview instead of the original first value i entered
If i entered John on the form and submit, i want to see John displays instead James that was enters before
Below is my Code in c#
Profile.aspx
<asp:TextBox ID="FirstName" runat="server" Width="200px"></asp:TextBox>
<asp:TextBox ID="LastName" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="SubmitBtn" runat="server" onclick="SubmitBtn_Click" Text="Submit" />
Profile.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitBtn_Click(object sender, EventArgs e)
{
SqlDataSource BknFSDataSource = new SqlDataSource();
BknFSDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["BknFSConnectionString"].ToString();
BknFSDataSource.InsertCommandType = SqlDataSourceCommandType.Text;
BknFSDataSource.InsertCommand = "INSERT INTO Profile(FirstName, LastName)VALUES(@FirstName, @LastName)";
BknFSDataSource.InsertParameters.Add("FirstName", FirstName.Text);
BknFSDataSource.InsertParameters.Add("LastName", LastName.Text);
int rowsAffected = 0;
try
{
rowsAffected = BknFSDataSource.Insert();
}
catch (Exception ex)
{
Server.Transfer("Error.aspx");
}
finally
{
BknFSDataSource = null;
}
if (rowsAffected != 1)
{
Server.Transfer("Error.aspx");
}
else
{
Server.Transfer("Successfull.aspx");
}
}
}
Successfull.aspx
<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False"
DataKeyNames="AcctID" DataSourceID="ProfileForm"
EnableModelValidation="True" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="AcctID" HeaderText="AcctID" InsertVisible="False"
ReadOnly="True" SortExpression="AcctID" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="ProfileForm" runat="server"
ConnectionString="<%$ ConnectionStrings:BknFSConnectionString %>"
SelectCommand="SELECT [AcctID], [LastName] FROM [Profile]">
</asp:SqlDataSource>