I have to call a function on a button click event like this
private void btnConnect_Click(object sender, EventArgs e)
{
string localHost = "192.168.10.3";
string logInDetails = "gp";
//SqlConnection sConnection = new
//SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
try
{
//Checking for the Valid entries in textboxes.
if ((txtPassword.Text == logInDetails) && (txtUsername.Text == logInDetails))
//Checking for the appropriate local server address.
if (txtHost.Text == localHost)
{
BindDBDropDown();
SetOperationDropDown();
PrimaryKeyTable();
lblError.Text = "You are connected to the SQL Server....";
}
else
{
lblError.Text = "Invalid Credentials";
}
}
catch (Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
}
//finally
//{
// //To close the connection
// if (sConnection != null)
// {
// sConnection.Close();
// }
//}
}
the BindDBDropDown function definition is in separate class like
public class DataAccessMaster
{
/// <summary>
/// This function gets the list of all the databases present in the local server.
/// </summary>
/// <returns></returns>
public static DataSet GetAllDataBaseNames()
{
SqlConnection sConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
//To Open the connection.
sConnection.Open();
string selectDatabase = @"SELECT
[NAME]
FROM
[master..sysdatabases]";
SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection);
try
{
DataSet dsListOfDatabases = new DataSet("master..sysdatabases");
SqlDataAdapter da = new SqlDataAdapter(selectDatabase, sConnection);
da.TableMappings.Add("Table", "master..sysdatabases");
da.Fill(dsListOfDatabases);
DataViewManager dsv = dsListOfDatabases.DefaultViewManager;
return dsListOfDatabases;
}
catch (Exception ex)
{
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
return null;
}
finally
{
//To close the connection
if(sConnection != null)
{
sConnection.Close();
}
}
and I called this function like this
public void BindDBDropDown()
{
DataSet dsDatabaseList = default(DataSet);
try
{
//The function GetAllDataBaseNames() is called from the DataAccessMaster class.
dsDatabaseList = DataAccessMaster.GetAllDataBaseNames();
//Path to the combo box is given to get the value through the GetAllDataBaseNames().
cmbDatabases.DataSource = dsDatabaseList.Tables["master..sysdatabases"];
cmbDatabases.DisplayMember = "NAME";
cmbDatabases.ValueMember = ("");
}
But it is not binding the required list in the drop down.
Can you guys plz help me!!
I’m guessing the problem is largely here:
If anything goes wrong, you are swallowing it (returning
null). You don’t show what thecatchis inBindDBDropDown, but I’m guessing it is similar…