Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to get the button id

what i actually want is
i am having two forms on 1st form form1 i am having two buttons when ever any of the two button is clicked and it will open 2nd form form2. Now on form2 i want to display which button was pressed. i was wondering if i could get the buttonID. to the next page

can you please help me

What I have tried:

on form1

private void btnAddState_Click(object sender, EventArgs e)
{
ButtonClicked.buttonClicked = (sender as Button).Text;
Add add = new Add();
add.Show();
}

on Form2

private void Add_Load(object sender, EventArgs e)
{
connection();
if (!string.IsNullOrEmpty(ButtonClicked.buttonClicked))
{
buttonID = ButtonClicked.buttonClicked;
}
}

private void btnAdd_Click(object sender, EventArgs e)
{
if (buttonID == "btnAddState")
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.CommandText = "insert into state(State_Name) values('" + txtName + "')";
cmd.ExecuteNonQuery();
}
}
Posted
Updated 10-Jun-19 3:40am

The simple way is to change the Form2 constructor to accept the string and pass it when you create the instance.

But ... don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Comments
MukulMohal 8-Jun-19 5:50am    
but how i can get the button id form one page to another page.
OriginalGriff 8-Jun-19 6:04am    
Read what I said:
"The simple way is to change the Form2 constructor to accept the string and pass it when you create the instance."

for this i figured out a way to pass a string to the form 2 on button_click event which is on form 1
and its working for me
 
Share this answer
 
Comments
CHill60 10-Jun-19 11:18am    
This is not a good solution. You have not shown the code that works
Dave Kreskowiak 10-Jun-19 12:17pm    
From your description, this isn't even a good solution. A form should NEVER know anything about the controls of another form, and apparently, your Form2 has to know what the button names are on Form1. That's bad because you have now permanently tied Form2 to Form1. Form2 can now never work from any other form except Form1.
MukulMohal 11-Jun-19 0:45am    
from here i learnt how to pass data from one form to another form

https://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms

and i used property method to pass id from one page to another page by mannually setting the id to the property.

private void btnAddState_Click(object sender, EventArgs e)
        {
            BtnID = "btnAddState";
            using (Add add = new Add())
            {
                add.buttonID = BtnID;
                add.ShowDialog();
            }
        }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900