简体   繁体   中英

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

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

  1. Declare string builder var as class level
  2. On button click append button text to string builder
  3. finally use string builder var as a result of all button click's

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

public void Button31_Click(object sender, EventArgs e)
{
   string sql = @"Insert into [dbo].[BookedTickets] 
   ([SeatNo],[Status])values('"+ btnsText+"','true')";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM