|
Can you post the code? It doesn't matter what you do in the code, but it's important that you model the flow so that methods exit nicely and the calling method understands not to continue with execution. But as said, it's a bit hard to say more without the code.
|
|
|
|
|
// written in class file
public void mthOpenConnection()
{
try
{
string[] datasource = File.ReadAllLines("d:\\k2.txt");
if (datasource[0] != "")
{
objprop.connection = "Data Source=" + datasource[0] + ";Initial Catalog=Inventory_VS;User ID=sa;Password=sa";
objCon = new SqlConnection(objprop.connection);
if (objCon.State != ConnectionState.Open)
{
objCon.Open();
}
}
}
catch
{
MessageBox.Show("ENTER CORRECT DATA SOURCE NAME IN UR TEXT FILE");
}
}
// this is my login form here i call my open connection
public DataSet mthGetLogin(Properties objproperties)
{
objDBconnection.mthOpenConnection();
SqlDataAdapter objDALogin = new SqlDataAdapter("sp_login", objDBconnection.objCon);
objDALogin.SelectCommand.Parameters.Add(new SqlParameter("@uname", objproperties.Username));
objDALogin.SelectCommand.Parameters.Add(new SqlParameter("@pword", objproperties.Password));
objDALogin.SelectCommand.CommandType = CommandType.StoredProcedure;
objDALogin.Fill(objDSLogin);
return objDSLogin;
}
|
|
|
|
|
Okay, you could use for example bools:
public bool mthOpenConnection()
{
try
{
string[] datasource = File.ReadAllLines("d:\\k2.txt");
if (datasource[0] != "")
{
objprop.connection = "Data Source=" + datasource[0] + ";Initial Catalog=Inventory_VS;User ID=sa;Password=sa";
objCon = new SqlConnection(objprop.connection);
if (objCon.State != ConnectionState.Open)
{
objCon.Open();
}
}
}
catch
{
MessageBox.Show("ENTER CORRECT DATA SOURCE NAME IN UR TEXT FILE");
return false;
}
return true;
}
and then
public DataSet mthGetLogin(Properties objproperties)
{
if (!objDBconnection.mthOpenConnection())
{
return null;
}
SqlDataAdapter objDALogin = new SqlDataAdapter("sp_login", objDBconnection.objCon);
objDALogin.SelectCommand.Parameters.Add(new SqlParameter("@uname", objproperties.Username));
objDALogin.SelectCommand.Parameters.Add(new SqlParameter("@pword", objproperties.Password));
objDALogin.SelectCommand.CommandType = CommandType.StoredProcedure;
objDALogin.Fill(objDSLogin);
return objDSLogin;
}
|
|
|
|
|
it works fine...
but the flow didn't stoped bcz next consequetive lines needs valid open connection...
(but i catch invalid connection in my try block)(contradictory)
it ends in error...
|
|
|
|
|
If you added the modification to mthGetLogin that I wrote, the execution in that method won't continue because I added return null to it. However, this is only part of your code so you must use this idea in all places where you want to 'break' the execution
|
|
|
|
|
s u r right...
see this peace of code...
this is my loging forms's button(submit) click event...
when the program try to read table values error occurs...
private void button2_Click_1(object sender, EventArgs e)
{
objproperties.Username = txtuname.Text;
objproperties.Password = txtpword.Text;
objDSLogin = objfunction.mthGetLogin(objproperties);
if (objDSLogin.Tables[0].Rows.Count > 0)
{
if (objDSLogin.Tables[0].Rows[0]["status"].ToString() == "User")
{
objproperties.Username = txtuname.Text;
objfunction.mthUsernameInsert(objproperties);
this.Hide();
UserForm uf = new UserForm();
uf.Show();
}
else if (objDSLogin.Tables[0].Rows[0]["status"].ToString() == "Admin")
{
this.Hide();
AdminForm1 af = new AdminForm1();
af.Show();
}
else
{
label4.Text = "Login Failed... Try Again";
label4.Visible = true;
txtpword.Text = "";
txtuname.Focus();
}
}
else
{
label4.Visible = true;
txtpword.Text = "";
txtuname.Focus();
}
}
|
|
|
|
|
So based on the previous example I wrote, you could:
...
objproperties.Password = txtpword.Text;
objDSLogin = objfunction.mthGetLogin(objproperties);
if (objDSLogin == null)
{
return;
}
if (objDSLogin.Tables[0].Rows.Count > 0)
{
...
|
|
|
|
|
is anyother way available....
like goto,break,Abandon...
|
|
|
|
|
goto can go only to a specific point inside a method. break exits a loop so there's no quick way to do this. One way is to throw an exception inside a catch. Then catch this exception at the level you want.
|
|
|
|
|
how i can do this...
guide me..
|
|
|
|
|
public bool mthOpenConnection()
{
try
{
objprop.J = 0;
string[] datasource = File.ReadAllLines("d:\\k2.txt");
if (datasource[0] != "")
{
objprop.connection = "Data Source=" + datasource[0] + ";Initial Catalog=Inventory_VS;User ID=sa;Password=sa";
objCon = new SqlConnection(objprop.connection);
if (objCon.State != ConnectionState.Open)
{
objCon.Open();
}
}
}
catch (Exception ex)
{
MessageBox.Show("ENTER CORRECT DATA SOURCE NAME IN UR TEXT FILE");
return false;
}
return true;
}
how i can throw my exceptions....
|
|
|
|
|
how 2 display message to user in class file...(windows app using c#)
messagebox.show is not working.
|
|
|
|
|
In c#, MessageBox.Show() should work. Check the case of the statement you have. C# is case sensitive.
Bhaskara
|
|
|
|
|
in c# message box is working. it is not a problem. im using it in class file..
|
|
|
|
|
You cannot have a messagebox displayed in a class file.
Bhaskara
|
|
|
|
|
What do you mean by saying that messagebox is not working?
|
|
|
|
|
if i type sqlconnection in my form it will shows the functions starts with letter "s"(sqlcommmand,sqladaptorm,reader) and sqlconnection also...
when i type messagebox.show the option messagebox.show did not displayed
|
|
|
|
|
In class library project, reference to System.Windows.Forms is not added. MessageBox is a class that is available in System.Windows.Forms namespace which is only added to the windows applications.
Bhaskara
|
|
|
|
|
SO the Intellisense is not working. Is the code compiling anyway?
One thing could be that you haven't included the namespace (System.Windows.Forms) in you file by using directive. Also the Intellisense may often get messed, but usually restarting the development environment corrects that. If that doesn't help, you could set the intellisense off from options, restart visual studio and set it on again.
Also check that you have service packs installed for visual studio.
|
|
|
|
|
both of u got that
am not included the namespace
|
|
|
|
|
I didn't quite understand you reply, but if you haven't included the namespace, add
using System.Windows.Forms;
to the beginning of your file
|
|
|
|
|
added and executed successfully...
thx..
|
|
|
|
|
|
MessageBox.Show("your message goes here");
|
|
|
|
|
Since a class file shouldn't show any kind of user interface, you don't do this.
|
|
|
|