Hi, i have a problem. I try to create a non visual component. This is
a simple component that makes database connection. I use the following
code. But in design time i can't see the properties. I use public
properties. Everything seems ok but in design time i can not see the
properties that i have defined. I defined server name,database name,
user name and password as properties and all these should be given in
design time.
Here is the source code :
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data.SqlClient;
namespace DBConnect
{
public class dbCon : System.ComponentModel.Component
{
private System.ComponentModel.Container components = null;
private string serverName;
private string dataBaseName;
private string userName;
private string passWord;
private bool conStatus;
private SqlConnection con;
public dbCon(System.ComponentModel.IContainer container)
{
container.Add(this);
InitializeComponent();
}
public dbCon()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
[CategoryAttribute("Appearance"),BrowsableAttribute (true)]
public string ServerName
{
set
{
this.serverName=value;
}
}
[BrowsableAttribute(true)]
public string DataBase
{
set
{
this.dataBaseName=value;
}
}
[BrowsableAttribute(true)]
public string UserName
{
set
{
this.userName=value;
}
}
[BrowsableAttribute(true)]
public string PassWord
{
set
{
this.passWord=value;
}
}
[BrowsableAttribute(true)]
public bool isConnect
{
set
{
if(value)
{
if(openConnection())
this.conStatus=true;
}
else
{
if(closeConnection())
this.conStatus=false;
}
}
}
public SqlConnection Connection
{
get
{
return this.con;
}
}
private bool openConnection()
{
bool isException=false;
try
{
con=new SqlConnection();
con.ConnectionString="server="+serverName+";
database="+dataBaseName+"; uid="+userName+";pwd="+passWord;
con.Open();
}
catch(SqlException exp)
{
isException=true;
}
return isException;
}
private bool closeConnection()
{
if(con.State==System.Data.ConnectionState.Open)
{
try
{
con.Close();
con.Dispose();
}
catch(SqlException ex)
{
return false;
}
}
return true;
}
}
}