Hi...
1)How to use SQL Connection for C# Windows Application where i am having 3 Comboboxes ( all data dynamically binding from DB ) and 2nd Combobox depends on 1st and 3rd will depend on 2nd combobox...
Is it required to use every connection open in every method..
2) How to periodically know whether SQL Server is in Open or Closed Status
Loading
Sam HobbsPosted Oct 18, 2011, 5:03 PM
Dhaval PatelPosted Oct 18, 2011, 7:15 AM
protected void Page_Load(object sender, System.EventArgs e)
{
getcategorydetails();
}
public void getcategorydetails()
{
DataSet ds = new DataSet();
ds = getbuying_product(companyid, Strings.Replace(Strings.Trim(category.SelectedItem.Value), "'", "''"));
category.DataSource = ds.Tables[0];
category.DataTextField = ds.Tables[0].Columns["productname"].ColumnName.ToString();
category.DataValueField = ds.Tables[0].Columns["productcode"].ColumnName.ToString();
category.DataBind();
}
public void getproductdetails()
{
DataSet ds = new DataSet();
ds = getbuying_product(companyid, Strings.Replace(Strings.Trim(category.SelectedItem.Value), "'", "''"));
product.DataSource = ds.Tables[0];
product.DataTextField = ds.Tables[0].Columns["productname"].ColumnName.ToString();
product.DataValueField = ds.Tables[0].Columns["productcode"].ColumnName.ToString();
product.DataBind();
}
public void getquantitydetails()
{
DataSet ds = new DataSet();
ds = getbuying_product(companyid, Strings.Replace(Strings.Trim(category.SelectedItem.Value), "'", "''"));
quantity.DataSource = ds.Tables[0];
quantity.DataTextField = ds.Tables[0].Columns["productname"].ColumnName.ToString();
quantity.DataValueField = ds.Tables[0].Columns["productcode"].ColumnName.ToString();
quantity.DataBind();
}
public DataSet getbuying_product(string companyid, string categoryvalue)
{
string query = null;
query = "select c.categorycode , b.productname , b.productcode from brc_buying_productdetail c , brccategoryproductmaster b " + "where c.categorycode = b.categorycode and c.productcode = b.productcode and c.companyid='" + companyid + "' and c.categorycode='" + categoryvalue + "' and b.status='Y'";
DataSet ds = null;
ds = new DataSet();
ds = returndataset(query, CommandType.Text);
return ds;
}
protected void category_SelectedIndexChanged(object sender, System.EventArgs e)
{
getproductdetails();
}
protected void category_SelectedIndexChanged(object sender, System.EventArgs e)
{
getquantitydetails
}
and also check sql server status
fro sqlconnection sqlconn=new sqlconnection ()
sqlconn.open()
and also check in sql server sp_who which database is running
Abhimanyu K VatsaPosted Oct 8, 2011, 3:20 AM
Depends on method accessibility, in web apps we usually uses following
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["ArticlesConnectionString1"].ConnectionString;
}
//and then
SqlConnection myConnection = new SqlConnection(GetConnectionString());
ii) Sample Code
private void OnLoad(object sender, System.EventArgs e)
{
ListCategories();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListCategories1();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//work on next task here
}
private void ListCategories()
{
sqlCon = new SqlConnection();
sqlCon.ConnectionString = Common.GetConnectionString();
cmd = new SqlCommand();
cmd.Connection = sqlCon;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Categories";
sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = cmd;
ds = new DataSet();
try
{
sqlDa.Fill(ds, "Category");
DataRow nRow = ds.Tables["Category"].NewRow();
nRow["CategoryName"] = "List All";
nRow["CategoryID"] = "0";
ds.Tables["Category"].Rows.InsertAt(nRow, 0);
//Binding the data to the combobox.
cmbCategory.DataContext = ds.Tables["Category"].DefaultView;
//To display category name (DisplayMember in Visual Studio 2005)
cmbCategory.DisplayMemberPath =
ds.Tables["Category"].Columns["CategoryName"].ToString();
//To store the ID as hidden (ValueMember in Visual Studio 2005)
cmbCategory.SelectedValuePath =
ds.Tables["Category"].Columns["CategoryID"].ToString();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while loading categories.");
}
finally
{
sqlDa.Dispose();
cmd.Dispose();
sqlCon.Dispose();
}
}
private void ListCategories1()
{
sqlCon = new SqlConnection();
sqlCon.ConnectionString = Common.GetConnectionString();
cmd = new SqlCommand();
cmd.Connection = sqlCon;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Categories";
sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = cmd;
ds = new DataSet();
try
{
sqlDa.Fill(ds, "Category");
DataRow nRow = ds.Tables["Category"].NewRow();
nRow["CategoryName"] = "List All";
nRow["CategoryID"] = "0";
ds.Tables["Category"].Rows.InsertAt(nRow, 0);
//Binding the data to the combobox.
cmbCategory.DataContext = ds.Tables["Category"].DefaultView;
//To display category name (DisplayMember in Visual Studio 2005)
cmbCategory.DisplayMemberPath =
ds.Tables["Category"].Columns["CategoryName"].ToString();
//To store the ID as hidden (ValueMember in Visual Studio 2005)
cmbCategory.SelectedValuePath =
ds.Tables["Category"].Columns["CategoryID"].ToString();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while loading categories.");
}
finally
{
sqlDa.Dispose();
cmd.Dispose();
sqlCon.Dispose();
}
}
(iii) Check SQL Server Open Close Status
you may try this, i'm not sure with this
if (oconn.State == ConnectionState.Closed)
Prabhu RajaPosted Oct 8, 2011, 2:59 AM
Try this. Check SQl Connection. you can use SelectionChangeCommited() Event handler of each Combo Boxes to change Datasource according to Selection.
Javeed M ShaikhPosted Oct 7, 2011, 11:07 AM
Please do not forget to mark "Accepted Answer".