I want to make a button(C# winform):
The code in the usercontrol:
public partial class UserControl1 : Button
{
string sqlstr;
[Description("SQL STRING")]
[DefaultValue(typeof(string), "")]
public string SqlStr
{
get { return sqlstr; }
set { sqlstr = value; }
}
private void button1_Click(object sender, EventArgs e)
// if I change this into overide OnClick( EventArgs e) the problem still //exist
{
base.OnClick(e);
String connstr = @"Provider=OraOLEDB.Oracle;data source=***;user id=***;password=***;";
OleDbConnection conn = new OleDbConnection(connstr);
string str = this.sqlstr;
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(str, conn);
cmd.ExecuteNonQuery();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
conn.Close();
}
}
}
The code in the main frame:
this.btn_del = new mybutton.UserControl1();
this.btn_del.Click += new System.EventHandler(this.btn_del_Click);
......
private void btn_del_Click(object sender, EventArgs e)
{
string txtid =txt_id.Text;// a label called txt_id in my frame
string strsql = "delete from myTable where id='" + txtid + "'";
btn_del.SqlStr = strsql;
}
Why doesn't the custom button work when the mainframe has more than one button? (only one button receive the SqlStr, others receive nothing) if there is another button in the frame private void btn_insert_Click(object sender, EventArgs e) { string txtid =txt_id.Text;// a label called txt_id in my frame string strsql = "insert into mytable (...)values (...)"; btn_insert.SqlStr = strsql; }
the SqlStr can not be received by the usercontrol
3' or 1=1; --
into your txt_id textbox would cause bad things to happen – Adam Rackis