public static string GetConnectionString(string strConnection)
{
//variable to hold our connection string for returning it
string strReturn = "";
//check to see if the user provided a connection string name
//this is for if your application has more than one connection string
if (!string.IsNullOrEmpty(strConnection)) //a connection string name was provided
{
//get the connection string by the name provided
strReturn = ConfigurationManager.ConnectionStrings[strConnection].ConnectionString;
}
else //no connection string name was provided
{
//get the default connection string
strReturn = ConfigurationManager.ConnectionStrings["SQLBasic.Properties.Settings.MovieDBConnectionString"].ConnectionString;
}
//return the connection string to the calling method
return strReturn;
}
/// <summary>
/// Returns a BindingSource, which is used with, for example, a DataGridView control
/// </summary>
/// <param name="cmdSql">"pre-Loaded" command, ready to be executed</param>
/// <returns>BindingSource</returns>
/// <remarks>Use this function to ease populating controls that use a BindingSource</remarks>
public static BindingSource GetBindingSource(SqlCommand SQLCommand)
{
BindingSource oBindingSource = new BindingSource();
SqlDataAdapter daGet = new SqlDataAdapter(SQLCommand);
DataTable dtGet = new DataTable();
SQLCommand.CommandTimeout = 240;
dtGet.Locale = System.Globalization.CultureInfo.InvariantCulture;
try
{
daGet.Fill(dtGet);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error in GetBindingSource");
return null;
}
oBindingSource.DataSource = dtGet;
return oBindingSource;
}
public static void HandleConnection(SqlConnection oCn)
{
//do a switch on the state of the connection
switch (oCn.State)
{
case ConnectionState.Open: //the connection is open
//close then re-open
oCn.Close();
oCn.Open();
break;
case ConnectionState.Closed: //connection is open
//open the connection
oCn.Open();
break;
default:
oCn.Close();
oCn.Open();
break;
}
}
My question is on the GetBindingSource method. Does it work by just passing it the name of the stored procedure name and it'll return the binding source for me to attach to a DataGridView?
I'm reading the Tut by PsychoCoder but I see he creates a connection string in a lot of methods and creates a command in all the methods. Isn't it better to just create 1 connection string and one binding source so you pass on the Command to that method?
Thanks for the information.

New Topic/Question
Reply



MultiQuote



|