public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlconPubs;
//SqlDataReader sdasearch;
SqlDataAdapter sqldaSearch;
DataSet dsetSearch;
DataTable dtableSearch;
string tableName;
string SearchField;
protected void Page_Load(object sender, EventArgs e)
{
sqlconPubs = new SqlConnection("database=ganga; server=SYSTEM; trusted_connection=true;");
//sqlconPubs.Open();
//SqlDataReader sdasearch = new SqlDataReader();
//dsetSearch = new DataSet();
sqldaSearch = new SqlDataAdapter();
dsetSearch = new DataSet();
tableName = "titles";
if (radPublisher.Checked)
tableName = "pubs";
SearchField =ddlOrderBy.Text;
AddItems(tableName);
gvResults.DataSource = null;
gvResults.DataBind();
sqlconPubs.Close();
}
protected void AddItems(string table)
{
//SqlCommand cmd = new SqlCommand("select * from" + tableName, sqlconPubs);
//sqlconPubs.Close();
//sqlconPubs.Open();
//sdasearch = cmd.ExecuteReader();
//sqlconPubs.Open();
//SqlDataReader sdasearch = cmd.ExecuteReader();
sqldaSearch = new SqlDataAdapter("select * from " + table, sqlconPubs);
sqldaSearch.Fill(dsetSearch, table);
ddlOrderBy.Items.Clear();
//ddlOrderBy.DataSource = null;
dtableSearch = dsetSearch.Tables[table];
ddlOrderBy.DataSource = dtableSearch.Columns;
ddlOrderBy.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string selectCommand;
if (txtKeywords.Text == "")
selectCommand = @"select * from " + tableName + SearchField + " " + ddlOrderBy.SelectedItem.Value;
else
selectCommand = @"select * from " + tableName + " Where" + SearchField + "like '" + txtKeywords.Text + ddlOrderBy.SelectedItem.Value;
sqldaSearch = new SqlDataAdapter(selectCommand, sqlconPubs);
dsetSearch.Clear();
dtableSearch = new DataTable();
sqldaSearch.Fill(dtableSearch);
//sqldaSearch.Fill(dsetSearch, tableName);
//dtableSearch = dsetSearch.Tables[tableName];
//SqlCommand cmd = new SqlCommand("select *from title where title like 'N%'", sqlconPubs);
//SqlCommand cmd = new SqlCommand("select * from pubs whjere city like 'B%'", sqlconPubs);
SqlCommand cmd = new SqlCommand();
cmd.CommandText="select * from tableName
gvResults.DataSource = dtableSearch;
gvResults.DataBind();
ddlOrderBy.Text = SearchField;
}
protected void radTitle_CheckedChanged(object sender, EventArgs e)
{
if(radTitle.Checked)
{
tableName="titles";
SearchField="title";
AddItems(tableName);
}
}
protected void radPublisher_CheckedChanged(object sender, EventArgs e)
{
if(radPublisher.Checked)
{
tableName = "pubs";
SearchField = "City";
AddItems(tableName);
}
}
}