Hi Vadivel892,
Check this example. Now please take its reference and correct your code.
HTML
Reservation
<table>
<tr>
<th>Name</th>
<td>
<asp:TextBox ID="txtName" runat="server" Text="Vadivel" /></td>
</tr>
<tr>
<th>Mobile</th>
<td>
<asp:TextBox ID="txtMobile" runat="server" Text="12345678" /></td>
</tr>
<tr>
<th>Email</th>
<td>
<asp:TextBox ID="txtEmail" runat="server" Text="Vadivel892@gmail.com" /></td>
</tr>
<tr>
<th>Slot</th>
<td>
<asp:TextBox ID="txtSlot" runat="server" Text="1" /></td>
</tr>
<tr>
<th>Amount</th>
<td>
<asp:TextBox ID="txtAmount" runat="server" Text="1000" /></td>
</tr>
<tr>
<td colspan="2" align="Center">
<asp:Button Text="Save" runat="server" OnClick="OnSave" /></td>
</tr>
</table>
Success
<asp:GridView runat="server" ID="gvEventBooked"></asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
Reservation
protected void OnSave(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string mobile = txtMobile.Text.Trim();
string email = txtEmail.Text.Trim();
string slot = txtSlot.Text.Trim();
decimal amount = Convert.ToDecimal(txtAmount.Text.Trim());
string date = DateTime.Now.ToString();
string time = DateTime.Now.ToShortTimeString();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO EventBookingslots (Cus_Name,Cus_Mobile,Cus_Email,Booked_Slots,Amount,Event_Date,Event_Time) " +
"VALUES (@Name,@Mobile,@Email,@Slot,@Amount,@Date,@Time)" +
"SELECT SCOPE_IDENTITY()";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Mobile", mobile);
cmd.Parameters.AddWithValue("@Email", email);
cmd.Parameters.AddWithValue("@Slot", slot);
cmd.Parameters.AddWithValue("@Amount", amount);
cmd.Parameters.AddWithValue("@Date", date);
cmd.Parameters.AddWithValue("@Time", time);
cmd.Connection = con;
con.Open();
object id = cmd.ExecuteScalar();
con.Close();
if (id != null)
{
Session["LastID"] = id;
Response.Redirect("Success.aspx");
}
}
}
Success
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["LastID"] != null)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM EventBookingslots WHERE ID = @Id";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@Id", Session["LastID"].ToString());
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvEventBooked.DataSource = dt;
gvEventBooked.DataBind();
}
}
}
}
}
}
VB.Net
Reservation
Protected Sub OnSave(sender As Object, e As EventArgs)
Dim name As String = txtName.Text.Trim()
Dim mobile As String = txtMobile.Text.Trim()
Dim email As String = txtEmail.Text.Trim()
Dim slot As String = txtSlot.Text.Trim()
Dim amount As Decimal = Convert.ToDecimal(txtAmount.Text.Trim())
Dim _date As String = DateTime.Now.ToString()
Dim time As String = DateTime.Now.ToShortTimeString()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO EventBookingslots (Cus_Name,Cus_Mobile,Cus_Email,Booked_Slots,Amount,Event_Date,Event_Time) " &
"VALUES (@Name,@Mobile,@Email,@Slot,@Amount,@Date,@Time)" & "SELECT SCOPE_IDENTITY()"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Mobile", mobile)
cmd.Parameters.AddWithValue("@Email", email)
cmd.Parameters.AddWithValue("@Slot", slot)
cmd.Parameters.AddWithValue("@Amount", amount)
cmd.Parameters.AddWithValue("@Date", _date)
cmd.Parameters.AddWithValue("@Time", time)
cmd.Connection = con
con.Open()
Dim id As Object = cmd.ExecuteScalar()
con.Close()
If id IsNot Nothing Then
Session("LastID") = id
Response.Redirect("Success.aspx")
End If
End Using
End Sub
Success
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Session("LastID") IsNot Nothing Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM EventBookingslots WHERE ID = @Id"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Id", Session("LastID").ToString())
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvEventBooked.DataSource = dt
gvEventBooked.DataBind()
End Using
End Using
End Using
End If
End If
End Sub