Soldato
- Joined
- 20 Jan 2005
- Posts
- 2,722
- Location
- Whitley Bay
Hi there,
I've just started to learn C# so go easy on me (I'm used to VB.NET).
I'm creating a Logon window for an application which connects to the local SQL instance and changes a label on the form to state whether the connection was successful or not.
I know the connection is successful but the label isn't changing (I have a MessageBox set to show as a backup while I figure this out).
I think I'm using encapsulation correctly in that label1 is private on the Logon form, with a public string Label1 specifying the get and set properties.
I'm not sure the issue lies with this anyway, as I got the same issue if I set label1 to public.
The SQL code is specified in a separate class ConnectSQL:
I have a timer set to run on the load event of Logon which calls the ConnectSQL Connect method:
If I throw a button onto the Logon form and set the click event to change Label1.text it works fine, but nothing happens as part of the Connect method.
I appreciate this code is probably pretty poor, but I'm just starting out so if you can point me in the right direction I'd appreciate it!
Thanks
Si
I've just started to learn C# so go easy on me (I'm used to VB.NET).
I'm creating a Logon window for an application which connects to the local SQL instance and changes a label on the form to state whether the connection was successful or not.
I know the connection is successful but the label isn't changing (I have a MessageBox set to show as a backup while I figure this out).
I think I'm using encapsulation correctly in that label1 is private on the Logon form, with a public string Label1 specifying the get and set properties.
I'm not sure the issue lies with this anyway, as I got the same issue if I set label1 to public.
The SQL code is specified in a separate class ConnectSQL:
Code:
class ConnectSQL
{
public void Connect(string myConnectionString)
{
// If the connection string is null, use a default.
if (myConnectionString == "")
{
myConnectionString = "user id=*****;password=*****;server=localhost\\instancename;database=dbname";
}
SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
Logon logon = new Logon();
try
{
myConnection.Open();
if (myConnection.State == ConnectionState.Open)
logon.Label1 = "Connected";
MessageBox.Show("success");
}
catch (Exception)
{
logon.Label1 = "Connection Failed";
MessageBox.Show("Failed");
}
}
I have a timer set to run on the load event of Logon which calls the ConnectSQL Connect method:
Code:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
ConnectSQL connectnow = new ConnectSQL();
connectnow.Connect("connectionstringdetails");
}
If I throw a button onto the Logon form and set the click event to change Label1.text it works fine, but nothing happens as part of the Connect method.
I appreciate this code is probably pretty poor, but I'm just starting out so if you can point me in the right direction I'd appreciate it!
Thanks
Si
