Search

Tuesday, April 7, 2015
0 comments

Ticket booking in asp.net c#

4:57 AMTuesday, April 7, 2015
Before doing this first to learn how to register and login into the site because without login you can not book your ticket. This code is used for in any project (Online movie ticketing, railway reservation, airline reservation, etc), which you want to design it. I will take simple method for design this. First to design database table for this type of application. I have a table, which query is mentioned bellow:

I method


Ticket booking in asp.net c#

CREATE TABLE [dbo].[Booking] (
[BookingId] INT IDENTITY (1, 1) NOT NULL,
[seatno] NVARCHAR (MAX) NULL,
[userId] INT NULL,
[date] NVARCHAR (MAX) NULL,
[movie_id] INT NULL,
PRIMARY KEY CLUSTERED ([BookingId] ASC)
);

After design the database table you can insert seat_no in it with some specific data, such as userId, date, movie_id. Before inserting data into table, first to check whether the seat_no is already reserved ot not. check this mentioned bellow code for this:


private bool checkseat()
{
string seats=seattxt.Text; 
string[] seatres=seats .Split(','); 
cmd.CommandText = "select * from [Booking]"; 
cmd.Connection = con;
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read()) 
{
string alrbook = rd["seatno"].ToString(); 
string[] reseat = alrbook.Split(','); 
foreach (string item in seatres)
{
foreach (string item2 in reseat) 
{
if (item==item2) 
{

flag =false;

break;
}
}

}
}
rd.Dispose();
cmd.Parameters.Clear();
if(flag ==false)
return true;
else
return false;

}

Suppose your entered seat_no is not matched with already reserved seat_no then you can book new ticked, which is enter in TextBox. Otherwise you are not permitted.


if (checkseat() == false)

{
cmd.CommandText = "insert into [Booking](seatno,userId,date,movie_id)values(@seatno1,@userid1,@date1,@movieid1)";
cmd.Connection = con;

seat_amount = seat_amount * count;
cmd.Parameters.AddWithValue("@seatno1", s);
cmd.Parameters.AddWithValue("@userid1", 1);
cmd.Parameters.AddWithValue("@date1", mdatelbl.Text);
cmd.Parameters.AddWithValue("@movieid1", 1);

amounttxt.Text = seat_amount.ToString();

cmd.ExecuteNonQuery();
}
else
{
Response .Write ("Change seat no");

}
already reserved seat_no

II method to reserve the ticket

In the second method you can save your ticket separately in different rows.

you can save your ticket separately in different rows

    In this method first to check Seat_availability, which is mentioned above. Only single statement should change in above mentioned code that is:

private bool checkseat()
 {
 string seats=seattxt.Text;
 string[] seatres=seats .Split(',');
 cmd.CommandText = "select * from [Booking]";
 cmd.Connection = con;
 SqlDataReader rd = cmd.ExecuteReader();
 while (rd.Read())
 {
 string alrbook = rd["seatno"].ToString();
 
 foreach (string item in seatres)
 {
 
 if (item==alrbook)
 {

 flag =false;
 
 break;
 }
 
 
 }


 }
 rd.Dispose();
 cmd.Parameters.Clear();
 if(flag ==false)
 return true;
 else
 return false;
 
 }

Now, you can book your ticket , if seat is available. Create the parameter inside the foreach loop and save the value one by one


foreach (string item in s1)
 {
 
 cmd.CommandText = "insert into [Booking](seatno,userId,date,movie_id)values(@seatno"+c1+",@userid"+c1+",@date"+c1+",@movieid"+c1+")";


 seat_amount = seat_amount * count;
 cmd.Parameters.AddWithValue("@seatno"+c1, item);
 cmd.Parameters.AddWithValue("@userid"+c1, 1);
 cmd.Parameters.AddWithValue("@date"+c1, mdatelbl.Text);
 cmd.Parameters.AddWithValue("@movieid"+c1, 1);
 cmd.ExecuteNonQuery();
 c1++;
 
 }

In the next example, i will show you, how to cancel the ticket easily. Only those ticket will be removed, which is reserved by the user.
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top