How can I add the name of each button click to a string builder using C#
Clash Royale CLAN TAG#URR8PPP
How can I add the name of each button click to a string builder using C#
I'm writing a code for ticket booking. If suppose there are 30 seats and I have added the Button_click for each of the seats. I'm able to get the name of each seat. But I'm not sure how to add the button name of each button to the string builder and so that I can add it to the table one after the other when I press save(Button31_Click) button.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Ticketbooking
public partial class WebForm1: System.Web.UI.Page
public void Page_Load(object sender, EventArgs e)
string Seatname = "";
String output = "";
string sql = @"select* from[dbo].[BookedTickets]";
string connectionstring = @"Data
Source(local);InitialCatalog=master;Integrated
Security=SSPI";
SqlConnection cnn = new
SqlConnection(connectionstring);
cnn.Open();
SqlCommand command = new SqlCommand(sql, cnn);
SqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
output = output + dataReader.GetValue(0) +
dataReader.GetValue(1);
StringBuilder strbuild = new StringBuilder();
strbuild.Append(Seatname);
public void Button1_Click(object sender, EventArgs e)
Button butt1 = (Button)sender;
string Seatname= butt1.Text;
public void Button2_Click(object sender, EventArgs e)
Button butt2 = (Button)sender;
string Seatname= butt2.Text;
public void Button3_Click(object sender, EventArgs e)
Button butt3 = (Button)sender;
string Seatname = butt3.Text;
public void Button4_Click(object sender, EventArgs e)
Button butt4 = (Button)sender;
string Seatname = butt4.Text;
public void Button5_Click(object sender, EventArgs e)
Button butt5 = (Button)sender;
string Seatname = butt5.Text;
public void Button6_Click(object sender, EventArgs e)
Button butt6 = (Button)sender;
string Seatname= butt6.Text;
public void Button7_Click(object sender, EventArgs e)
Button butt7 = (Button)sender;
string Seatname= butt7.Text;
public void Button8_Click(object sender, EventArgs e)
Button butt8 = (Button)sender;
string Seatname = butt8.Text;
public void Button9_Click(object sender, EventArgs e)
Button butt9 = (Button)sender;
string Seatname = butt9.Text;
public void Button10_Click(object sender, EventArgs e)
Button butt10 = (Button)sender;
string Seatname = butt10.Text;
public void Button11_Click(object sender, EventArgs e)
Button butt11 = (Button)sender;
string Seatname= butt11.Text;
public void Button12_Click(object sender, EventArgs e)
Button butt12 = (Button)sender;
string Seatname = butt12.Text;
public void Button13_Click(object sender, EventArgs e)
Button butt13 = (Button)sender;
string Seatname = butt13.Text;
public void Button14_Click(object sender, EventArgs e)
Button butt14 = (Button)sender;
string Seatname = butt14.Text;
public void Button15_Click(object sender, EventArgs e)
Button butt15 = (Button)sender;
string Seatname= butt15.Text;
public void Button16_Click(object sender, EventArgs e)
Button butt16 = (Button)sender;
string Seatname= butt16.Text;
public void Button17_Click(object sender, EventArgs e)
Button butt17 = (Button)sender;
string Seatname = butt17.Text;
public void Button18_Click(object sender, EventArgs e)
Button butt18 = (Button)sender;
string Seatname= butt18.Text;
public void Button19_Click(object sender, EventArgs e)
Button butt19 = (Button)sender;
string Seatname= butt19.Text;
public void Button20_Click(object sender, EventArgs e)
Button butt20 = (Button)sender;
string Seatname= butt20.Text;
public void Button23_Click(object sender, EventArgs e)
Button butt23 = (Button)sender;
string Seatname= butt23.Text;
public void Button21_Click(object sender, EventArgs e)
Button butt21 = (Button)sender;
string Seatname= butt21.Text;
public void Button22_Click(object sender, EventArgs e)
Button butt22 = (Button)sender;
string Seatname = butt22.Text;
public void Button24_Click(object sender, EventArgs e)
Button butt24 = (Button)sender;
string Seatname = butt24.Text;
public void Button25_Click(object sender, EventArgs e)
Button butt25 = (Button)sender;
string Seatname= butt25.Text;
public void Button26_Click(object sender, EventArgs e)
Button butt26 = (Button)sender;
string Seatname= butt26.Text;
public void Button27_Click(object sender, EventArgs e)
Button butt27 = (Button)sender;
string Seatname = butt27.Text;
public void Button28_Click(object sender, EventArgs e)
Button butt28 = (Button)sender;
string Seatname= butt28.Text;
public void Button29_Click(object sender, EventArgs e)
Button butt29 = (Button)sender;
string Seatname= butt29.Text;
public void Button30_Click(object sender, EventArgs e)
Button butt30 = (Button)sender;
string Seatname = butt30.CommandName;
public void Button31_Click(object sender, EventArgs e)
string sql = @"Insert into [dbo].[BookedTickets]
([SeatNo],[Status])values('"+ Seatname+"','true')";
string connectionstring = @"Data Source=(local);Initial
Catalog=master;Integrated Security=SSPI";
SqlConnection cnn = new
SqlConnection(connectionstring);
cnn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sql, cnn);
DataTable dt = new DataTable();
adapter.Fill(dt);
Regards,
Sapna
3 Answers
3
Do not create 30 buttons, just create them dynamically:
string Seatname = "";
String output = "";
string sql = @"select* from[dbo].[BookedTickets]";
string connectionstring = @"Data
Source(local);InitialCatalog=master;Integrated
Security=SSPI";
SqlConnection cnn = new
SqlConnection(connectionstring);
cnn.Open();
SqlCommand command = new SqlCommand(sql, cnn);
SqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
Button btn = new Button();
btn.Text = SeatNameorWhatEver // set any properties
btn.Click += (s,e) =>
Button bt = (Button)s;
string Seatname = bt.Text;
;
Of course, you have to set each buttons location. Add it to Form
or Panel
or whatever
Form
Panel
I doubt whether I have to add a method for each button click?
– Sapna
Aug 6 at 9:21
(s,e) => Button bt = (Button)s; string Seatname = bt.Text; ;
this is actually the click method for all of them, (Button)s
will be the sender button.– Ashkan Mobayen Khiabani
Aug 6 at 9:26
(s,e) => Button bt = (Button)s; string Seatname = bt.Text; ;
(Button)s
May I suggest a List instead of a String(-Builder)? That way you can loop trough all selected values.
– Hans Kesting
Aug 6 at 7:13
Declare one string Globally in your class
string btnsText=string.Empty;
& call this method inside your all button click events to add clciekd button text
public string AppendSeats(string clickedText)
if(!btnsText.Contain(clickedText))
//Here adding space, you can add comma, dot etc..
btnsText = btnsText + " " + clickedText;
Like below you should call it
public void Button1_Click(object sender, EventArgs e)
Button butt1 = (Button)sender;
AppendSeats(butt1.Text);
Use here your btnsText
string
btnsText
public void Button31_Click(object sender, EventArgs e)
string sql = @"Insert into [dbo].[BookedTickets]
([SeatNo],[Status])values('"+ btnsText+"','true')";
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This will work. You need to be careful about viewstate though. If you add buttons dynamically you need to be aware that a postback, or (if applicable) a callback, can wipe out the viewstate of the buttons. If it's a callback make sure, that the buttons are not in the partially rendered area of the page or recreate the buttons on each page load
– Schadensbegrenzer
Aug 6 at 7:51