Wednesday, May 27, 2009

Product Selction Example

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace ViewStateApp
{

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList ddlPrimary;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.DropDownList ddlSecondary;
protected System.Web.UI.WebControls.RadioButton rdInclude;
protected System.Web.UI.WebControls.RadioButton rdExclude;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateProducts();
GetProductsToGrid();
}
}

private void InitializeComponent()
{
this.ddlPrimary.SelectedIndexChanged += new System.EventHandler(this.ddlPrimary_SelectedIndexChanged);
this.ddlSecondary.SelectedIndexChanged += new System.EventHandler(this.ddlSecondary_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
private void PopulateProducts()
{
ArrayList _ListItems = new ArrayList();
SqlConnection _Con = new SqlConnection("uid=sa;pwd=dev06;server=Dev06;database=winners");
_Con.Open();
DataSet ds = new DataSet();
SqlCommand _Cmd = new SqlCommand();
_Cmd.Connection = _Con;
_Cmd.CommandType = CommandType.StoredProcedure;
_Cmd.CommandText = "srispddlselect";
SqlDataAdapter da = new SqlDataAdapter(_Cmd);
da.Fill(ds);
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
_ListItems.Add(ds.Tables[0].Rows[i]["ProductName"].ToString());
}
ViewState["ProductName"] = _ListItems;

ddlPrimary.DataSource = _ListItems;
ddlPrimary.DataBind();
ddlSecondary.DataSource = _ListItems;
ddlSecondary.DataBind();
}
}
ddlPrimary.Items.Insert(0, new ListItem("--SelectOne--", "-1"));
ddlSecondary.Items.Insert(0, new ListItem("--SelectOne--", "-1"));

_Con.Close();

}

private void GetProductsToGrid()
{
SqlConnection _Con = new SqlConnection("uid=sa;pwd=dev06;database=winners;server=Dev06");
SqlCommand _Cmd = new SqlCommand();
DataSet ds = new DataSet();
_Con.Open();
_Cmd.Connection = _Con;
_Cmd.CommandType = CommandType.StoredProcedure;
_Cmd.CommandText = "SrispGetProductsToGrid";
SqlDataAdapter da = new SqlDataAdapter(_Cmd);
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

private void SaveProducts()
{
string str = "";
SqlConnection _Con = new SqlConnection("uid=sa;pwd=dev06;database=winners;server=Dev06");
SqlCommand _Cmd = new SqlCommand();
_Con.Open();
_Cmd.Connection = _Con;
_Cmd.CommandType = CommandType.StoredProcedure;
_Cmd.CommandText = "srik_saveproducts";
_Cmd.Parameters.Add("@PrimaryProduct", ddlPrimary.SelectedItem.Text);
_Cmd.Parameters.Add("@SecondaryProduct", ddlSecondary.SelectedItem.Text);
if (rdInclude.Checked)
{
str = "Include";
}
else
{
str = "Exclude";
}
_Cmd.Parameters.Add("@Status", str);
_Cmd.ExecuteNonQuery();


}

private void ddlPrimary_SelectedIndexChanged(object sender, System.EventArgs e)
{
ArrayList _ListItems=new ArrayList();
ArrayList _Lists=new ArrayList();
_Lists=(ArrayList)ViewState["ProductName"];
string s=ddlPrimary.SelectedItem.Text;
string str=ddlSecondary.SelectedItem.Text;
if(s!="--SelectOne--")
{
for(int i=0;i<_Lists.Count;i++)
{
if(s!=_Lists[i].ToString())
{
_ListItems.Add(_Lists[i].ToString());
}
}
}
else
{
_ListItems=_Lists;
}
ddlSecondary.DataSource=_ListItems;
ddlSecondary.DataBind();
ddlSecondary.Items.Insert(0,new ListItem("--SelectOne--","-1"));
ddlSecondary.Items.FindByText(str).Selected=true;

}

private void ddlSecondary_SelectedIndexChanged(object sender, System.EventArgs e)
{
ArrayList _ListItems=new ArrayList();
ArrayList _Lists=new ArrayList();
_Lists=(ArrayList)ViewState["ProductName"];
string s=ddlSecondary.SelectedItem.Text;
string str=ddlPrimary.SelectedItem.Text;
if(s!="--SelectOne--")
{
for(int i=0;i<_Lists.Count;i++)
{
if(s!=_Lists[i].ToString())
{
_ListItems.Add(_Lists[i].ToString());
}
}
}
else
{
_ListItems=_Lists;
}
ddlPrimary.DataSource=_ListItems;
ddlPrimary.DataBind();
ddlPrimary.Items.Insert(0,new ListItem("--SelectOne--","-1"));
ddlPrimary.Items.FindByText(str).Selected=true;

}

private void Button1_Click(object sender, System.EventArgs e)
{
SaveProducts();
}

No comments: