|
Do you really need to 'load them at runtime' (i.e. dynamically using Assembly.Load)? You only need to do that if you want a plugin based architecture. Simply linking to the DLLs at compile time should be sufficient in most cases.
If you do, you should implement some sort of lazy loading wrapper which loads an assembly only if it needs it. Though I think Assembly.Load probably already does that for you internally (don't quote me on that though).
You can only unload assemblies if they're loaded into separate AppDomains, which has serious implications for communication between modules.
|
|
|
|
|
When I try to click a button programmatically using .performClick() it only works when the button is in the same class
// this works
this.buttonStart.PerformClick();
But if I try to use the perform click outside the class it doesn't work
// this doesn' work
FormCalendarCS FCCS = new FormCalendarCS();
FCCS.buttonStart.PerformClick();
However if I use the following code I can access the button's method as follows, but doesn't click the button:
// this works
FCCS.buttonStart_Click(sender, e);
Why is this so, and how do I click on the button in another class.
I appreciate any help, thanks in advance.
Mihael
|
|
|
|
|
It seems like you have made buttonStart_Click public static in your FormCalendarCS class which is why you can access it (probably not a good idea!), whereas buttonStart is an instance created in the designer of your FormCalendarCS (exactly as it should be, check the contents of the designer.cs file).
Creating a new instance of the form will not get you the original button, just a new one on a new form that you haven't made visible so you will never see it.
First of all, change buttonStart_Click to private void buttonStart_Click(object sender, EventArgs e) , there is no valid reason to have it any other way IMO, and if you have changed it, set the access modifier on buttonStart back to private .
Inside FormCalendarCS create this method:
public void ButtonStartPerformClick()
{
buttonStart.PerformClick();
}
Inside your form where you first show your FormCalendarCS instance, add a field:
private FormCalendarCS formCalendarCS;
Before you show it, save the instance to the field.
formCalendarCS = new FormCalendarCS();
formCalendarCS.Show();
Now whenever you want to perform the click, just call:
formCalendarCS.ButtonStartPerformClick();
If the relationships between your classes/forms are more complex, you will need to route the method call(s) appropriately and you may need to use events too if you need to call back up the chain.
Your problem boils down to not understanding instance vs static objects and methods. These are a fundemental part of any OOP language including C# so I suggest some study on this area.
|
|
|
|
|
Thanks Dave,
I took your advice and did as you suggested, however, the button isn't static but was changed to public. I changed it back to private and accessed the button as you said. I also understand what you mean by a new instance of that class, I seem to forget that as I have did that more than once, only to realize my mistake.
Michael
|
|
|
|
|
I've never found a reason to use PerformClick, ever.
The code you put in the event handler really needs to be moved to it's own method. Then you can call it from the button Click event handler or from any outside caller without even touching the button.
|
|
|
|
|
I've never had a reason to use it either - the only advantage is it actually performs the click and so raises any other handlers attached to the Button 's Click event whereas calling the handling code would not do that.
|
|
|
|
|
MAW30 wrote: it only works when the button is in the same class Also, PerformClick() won't work if the button is disabled.
/ravi
|
|
|
|
|
why i cant store vales in database ???
private void Form8registration_Load(object sender, EventArgs e)
{
conn.Open();
}
private void ExecuteInsert(string empname, string desgination, string emailid, string phone, string skype, string address, string shifttime, string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
using (conn)
{
try
{
SqlCommand cmdd = new SqlCommand();
//cmdd.CommandType = CommandType.StoredProcedure;
cmdd.CommandText = "Insert into registrationn values (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
// cmdd.Parameters.Add("@empid", SqlDbType.VarChar, 150).Value = textBox7_empid.Text;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar, 150).Value = empname;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 150).Value = desgination;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value = skype;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery();
cmdd.Dispose();
}
catch (Exception ex)
{
string msg = "insert error ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
}
private void button2_reset_Click(object sender, EventArgs e)
{
Reset();
}
private void Reset()
{
textBox1_add.Text = "";
textBox3_phoneno.Text = "";
textBox4_emailid.Text = "";
textBox5_desgination.Text = "";
textBox6_name.Text = "";
textBox7_empid.Text = "";
comboBox1_shifttime.SelectedText= "";
textBox1_skpid.Text = "";
}
private void button3_cancel_Click(object sender, EventArgs e)
{
Close();
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text, textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
1 - Use Pre Tag To format your code, and make it readable.
2- In The Catch use
MessageBox.Show ( ex.Message)
So we can know what kind of error you get.
2 - Show me the Your Table Structure and Your Stored Procedure Code.
private void Form8registration_Load(object sender, EventArgs e)
{
conn.Open();
}
private void ExecuteInsert(string empname, string desgination, string emailid, string phone, string skype, string address, string shifttime, string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
using (conn)
{
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "Insert into registrationn values (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar, 150).Value = empname;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 150).Value = desgination;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value = skype;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery();
cmdd.Dispose();
}
catch (Exception ex)
{
string msg = "insert error ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
}
private void button2_reset_Click(object sender, EventArgs e)
{
Reset();
}
private void Reset()
{
textBox1_add.Text = "";
textBox3_phoneno.Text = "";
textBox4_emailid.Text = "";
textBox5_desgination.Text = "";
textBox6_name.Text = "";
textBox7_empid.Text = "";
comboBox1_shifttime.SelectedText= "";
textBox1_skpid.Text = "";
}
private void button3_cancel_Click(object sender, EventArgs e)
{
Close();
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text, textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
There is no stored procedure
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
and my stored procedure for the registration form is
ALTER PROCEDURE registrtn
(
@empname varchar(150),
@desgination varchar(150),
@emailid varchar(150),
@phone varchar(150),
@skype varchar(150),
@address varchar(150),
@shifttime varchar(150),
@panel varchar(150)
)
As
begin
INSERT INTO registrationn VALUES(@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)
select SCOPE_IDENTITY() as empid
end
|
|
|
|
|
..your command is filled with a sql-statement, and set to text. Also note that the using-statement would dispose your connection-object.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
this is my code can u tell where is the error so i can change ????
private void ExecuteInsert(string empname,string desgination,string emailid,string phone, string skype ,string address,string shifttime,string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel) VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar , 150).Value = empname ;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar,150).Value = desgination ;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid ;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone ;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value =skype ;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address ;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime ;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel ;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery ();
cmdd.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text , textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
Can you explain me why you are using the using directive, and what it actually does? And how are you testing the code, from the IDE (pressing F5) or standalone?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
yes am testing my code by pressing F5
and my stored procedure for the registration form is
ALTER PROCEDURE registrtn
(
@empname varchar(150),
@desgination varchar(150),
@emailid varchar(150),
@phone varchar(150),
@skype varchar(150),
@address varchar(150),
@shifttime varchar(150),
@panel varchar(150)
)
As
begin
INSERT INTO registrationn VALUES(@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)
select SCOPE_IDENTITY() as empid
end
am using using directive because when i run the program that time it may showing the error object reference not in instance of object, by using using this i can implement the structured in proper way
|
|
|
|
|
Right, the problem seems fairly straightforward. Basically, you are attempting to do an insert into the registration table without specifying the columns that will be inserted into. Now, the fact that you have a select in there for the scope_identity tells me that you have an identity column on that table - in other words, you have a column mismatch between what's in the table, and what you are inserting. To resolve this, all you need to do is explicitly define the columns you are inserting into in the insert statement.
|
|
|
|
|
YES i put identity column in my registration table and i also mentioned the column name in the insert query but the data is not displayed in the database when i executing the stored procedure and put the values then its inserted but when i put the value at the run time winform then it does not go to the database so tell me how can i do ???
|
|
|
|
|
see this i also mentioned the column name in insert query but data not display in the database
private void ExecuteInsert(string empname,string desgination,string emailid,string phone, string skype ,string address,string shifttime,string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel) VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar , 150).Value = empname ;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar,150).Value = desgination ;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid ;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone ;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value =skype ;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address ;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime ;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel ;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery ();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text.Trim() , textBox5_desgination.Text.Trim(), textBox4_emailid.Text.Trim(), textBox3_phoneno.Text.Trim(), textBox1_skpid.Text.Trim(), textBox1_add.Text.Trim(), comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
}
}
|
|
|
|
|
And what happens when you step over the code? Do you get an exception? Please, provide some more details - remember that we can't see what's on your computer screen, so you have to help us. Plus, is your data going into SQL Server or Sql CE? This makes a big difference - and how do you know the data hasn't inserted?
|
|
|
|
|
yes i passed message using exception in catch and now i get the error object reference not instance of object ? i will [provide you everything i cant check my data is going to sql server or sqlce because i dont know and the data is not stored in the database when i executing the form using breakpoint that time it display in the text or mentioned fields in code but when i go to database there is no value shown
how can i check my entered value is going to SQL server or not ????
and my code of the application private void ExecuteInsert(string empname,string desgination,string emailid,string phone, string skype ,string address,string shifttime,string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataReader rdr = null;
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel)" + " VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar , 150).Value = empname ;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar,150).Value = desgination ;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid ;
cmdd.Parameters.Add("@phone", SqlDbType.Int ).Value = Convert.ToString (phone) ;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value =skype ;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address ;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime ;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel ;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery ();
rdr = cmdd.ExecuteReader();
int jh;
int.TryParse(p1.Value.ToString(), out jh);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text , textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
////////here error showing object reference not an instance of object
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
Put a breakpoint on the line you have put in bold, and when the application breaks here, hover over each value in turn. See which one of those is giving you a null reference (which causes this error). BTW - you are attempting to execute a datareader in your code, and you have nothing to read into it.
|
|
|
|
|
sir when i put breakpoint on that line there is nowhere null show in any field and in every field have value that i had entered during the exucutime time of the program ...
|
|
|
|
|
HELLO SIR MY DATA NOT GOING TO SQL SERVER I DONT KNOW WHY MY CONNECTION STRING IS
<connectionstrings>
<add name="cn" connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;User Instance=True"
="" providername="System.Data.SqlClient">
|
|
|
|
|
There's no need to shout - we help out voluntarily, when our schedule permits. You need to step over your code and inspect the variables that are in there - one of them is null. We can't tell which one - only you can do that as you are the one sat in front of your computer.
If you inspect the full exception (use the .ToString() method on the exception rather than just the Message), you should get an idea on which line is at fault - and you can use that to narrow down your debugging.
|
|
|
|
|
my program is executed properly but insert and delete query is not working . i dont know why this not working . i checked the data that is going on the SQLServer but can not inserted in the database so tell my why its not inserted in the database
my inserted query is
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel)" + " VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar, 150).Value = empname;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 150).Value = desgination;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid;
cmdd.Parameters.Add("@phone", SqlDbType.Int).Value = Convert.ToString(phone);
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value = skype;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = Convert.ToString (shifttime);
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel;
//SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
//p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery();
|
|
|
|
|