how i get limited latest 6 rows from table - c#

i am trying to retrieve latest 6 rows from my database table ,i am using max value but how
i get limited 6 rows from table and that retrieved result to use in for loop,to display hyperlinks in panel
protected void Page_Load(object sender, EventArgs e)
{
String sql = "select title from up_song ";
SqlDataAdapter adpt = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
adpt.Fill(ds, "title");
if (ds.Tables["title"].Rows.Count > 0)
{
int m = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[8]);
int k = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[1]);
for (i=m; i >= k ; --i)
{
try
{
hp[i] = new HyperLink();
hp[i].ID = "hp" + i;
hp[i].Text = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
hp[i].NavigateUrl = "Downloadpage.aspx";
hp[i].ForeColor = System.Drawing.Color.White;
Panel1.Controls.Add(hp[i]);
Panel1.Controls.Add(new LiteralControl("<br>"));
HttpCookie coo = new HttpCookie("song");
coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
Response.Cookies.Add(coo);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
String sql1 = "select title from up_song where Song_type='Indian Pop Album'";
SqlDataAdapter adpt1 = new SqlDataAdapter(sql1, cn);
DataSet ds1 = new DataSet();
adpt1.Fill(ds1, "title");
if (ds1.Tables["title"].Rows.Count > 0)
{
String query = "select max(song_id) from up_song;";
SqlDataAdapter adpt2= new SqlDataAdapter(query,cn);
DataSet ds2= new DataSet();
adpt2.Fill(ds2,"max");
//int m = ds.Tables["title"].Rows.IndexOf(ds1.Tables["title"].Rows[query]);
//int k = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[1]);
for (i = 0; i <= ds2.Tables["max"].Rows.Count; ++i)
{
try
{
hp[i] = new HyperLink();
hp[i].ID = "hp" + i;
hp[i].Text = ds1.Tables["title"].Rows[i].ItemArray[0].ToString();
hp[i].NavigateUrl = "Downloadpage.aspx";
hp[i].ForeColor = System.Drawing.Color.White;
Panel2.Controls.Add(hp[i]);
Panel2.Controls.Add(new LiteralControl("<br>"));
HttpCookie coo = new HttpCookie("song");
coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
Response.Cookies.Add(coo);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
cn.Close();

select top 6 title from up_song order by song_id desc

Related

How to remain the treeview after redirect to another page?

Im currently using this method to bind my treeview, After i expand the treeview and click the node, it will redirect to another page, and then the treeview will be refresh to default, which means showing the treeview before expanding.. so i need to save the treeview state in order to remain the treeview state. i had tried the below method but it's not working, anyone can help..
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadTransactionMenu();
if (Session["ExpandedNodes"] != null)
((List<string>)Session["ExpandedNodes"]).ForEach(a => TreeView1.FindNode(a).Expanded = true);
}
}
private void LoadTransactionMenu()
{
//TRANSACTION MENU
string SubModuleID, SubModuleNM, DocModuleID, DocModuleName, MenuTransID, MenuTransName;
SqlConnection con = new SqlConnection(ConString);
string CmdString = "SELECT SUBMODULEID, SUBMODULENM, URLNAME FROM SUBMODULE";
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
TreeNode node1 = new TreeNode();
node1.Text = "TRANSACTION MENU";
TreeView1.Nodes.Add(node1);
for (int i = 0; i < dt.Rows.Count; i++)
{
SubModuleID = dt.Rows[i]["SUBMODULEID"].ToString();
SubModuleNM = dt.Rows[i]["SUBMODULENM"].ToString();
TreeNode SubModuleNode = new TreeNode(SubModuleNM, SubModuleID);
SubModuleNode.NavigateUrl = dt.Rows[i]["URLNAME"].ToString();
node1.ChildNodes.Add(SubModuleNode);
CmdString = "SELECT DDID, DOCNAME, DDADDRESS FROM DOCMODULE WHERE SUBMODULEID=#SUBMODULEID";
cmd = new SqlCommand(CmdString, con);
cmd.Parameters.AddWithValue("#SUBMODULEID", SubModuleID);
sda = new SqlDataAdapter(cmd);
DataTable dt2 = new DataTable();
sda.Fill(dt2);
for (int j = 0; j < dt2.Rows.Count; j++)
{
DocModuleID = dt2.Rows[j]["DDID"].ToString();
DocModuleName = dt2.Rows[j]["DOCNAME"].ToString();
TreeNode DocModuleNode = new TreeNode(DocModuleName, DocModuleID);
DocModuleNode.NavigateUrl = dt2.Rows[j]["DDADDRESS"].ToString();
SubModuleNode.ChildNodes.Add(DocModuleNode);
CmdString = "SELECT DID, DNAME, DADDRESS FROM MENUTRANSACTION WHERE DDID=#DDID AND UID= '" + Session["UserID"] + "'";
cmd = new SqlCommand(CmdString, con);
cmd.Parameters.AddWithValue("#DDID", DocModuleID);
sda = new SqlDataAdapter(cmd);
DataTable dt3 = new DataTable();
sda.Fill(dt3);
for (int k = 0; k < dt3.Rows.Count; k++)
{
MenuTransID = dt3.Rows[k]["DID"].ToString();
MenuTransName = dt3.Rows[k]["DNAME"].ToString();
TreeNode MenuTransNode = new TreeNode(MenuTransName, MenuTransID);
MenuTransNode.NavigateUrl = dt3.Rows[k]["DADDRESS"].ToString();
DocModuleNode.ChildNodes.Add(MenuTransNode);
}
if (DocModuleNode.ChildNodes.Count == 0)
{
SubModuleNode.ChildNodes.Remove(DocModuleNode);
}
}
if (SubModuleNode.ChildNodes.Count == 0)
{
node1.ChildNodes.Remove(SubModuleNode);
}
}
}
public void GetExpandedStatus(TreeNode node, List<string> ExpandedNodes)
{
//check if node is expanded
if (node.Expanded.GetValueOrDefault(false))
ExpandedNodes.Add(node.ValuePath);
node.ChildNodes.Cast<TreeNode>().ToList().ForEach(a => GetExpandedStatus(a, ExpandedNodes));
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
List<string> expandedNodes = new List<string>();
//get all expanded nodes
TreeView1.Nodes.Cast<TreeNode>().ToList().ForEach(a => GetExpandedStatus(a, expandedNodes));
//collapse all to reset the treeview
TreeView1.CollapseAll();
//save expanded node value paths
Session["ExpandedNodes"] = expandedNodes;
}

how to display the datavaluefield of selected items in different listbox in asp.net

I have 3 listboxes.The listbox fetches two values by code-datatextfield and datavaluefield. 1st listbox transfers the datatextfield items from 1st listbox to 2nd listbox.i want to transfer the datavaluefield of selected 1st listbox items to 3rd listbox.
if (ListBox3.SelectedIndex >= 0
{
for (int i = 0; i < ListBox3.Items.Count; i++)
{
if (ListBox3.Items[i].Selected)
{
if (!arraylist1.Contains(ListBox3.Items[i]))
{
arraylist1.Add(ListBox3.Items[i]);
Session["value"] = ListBox3.Items[i];
}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
{
ListBox2.Items.Add(((ListItem)arraylist1[i]));
}
ListBox3.Items.Remove(((ListItem)arraylist1[i]));
}
ListBox2.SelectedIndex = -1;
}
else
{
}
SqlConnection con = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("select * from IT_1_BOILER_DESK_1_PARAMETERS where paramtext='" + Session["value"] + "'", con);
SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
dataAadpter.Fill(ds);
if (ds != null)
{
ListBox1.DataSource = ds.Tables[0];
ListBox1.DataTextField = "param";
//ListBox3.DataValueField = "param";
ListBox1.DataBind();
}
for listbox 3
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("select * from IT_1_BOILER_DESK_1_PARAMETERS where pname='" + this.DropDownList1.SelectedValue + "'" ,con);
SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
dataAadpter.Fill(ds);
if (ds != null)
{
ListBox3.DataSource = ds.Tables[0];
ListBox3.DataTextField = "paramtext";
ListBox3.DataValueField = "param";
ListBox3.DataBind();
}
}
But i want to display the datavaluefield of the items that are selected from listbox3 to listbox1
Since you want to show DataTextField value and DataValueField value from one listbox into 2 different listbox. I suggest you to use a different approach. This will also reduce usage of 2nd database call.
Try following:-
if (ListBox3.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox3.Items.Count; i++)
{
if (ListBox3.Items[i].Selected)
{
if (!arraylist1.Contains(ListBox3.Items[i]))
{
arraylist1.Add(ListBox3.Items[i]);
//Session["value"] = ListBox3.Items[i]; no need of this
}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (ListBox2.Items.FindByText(((ListItem)arraylist1[i]).Text)==null)
{
//since you already have text and value field values in arrayList. Use them
ListBox2.Items.Add(new ListItem(((ListItem)arraylist1[i]).Text));
}
if (ListBox1.Items.FindByText(((ListItem)arraylist1[i]).Text)==null)
{
ListBox1.Items.Add(new ListItem((ListItem)arraylist1[i]).Value));
}
ListBox3.Items.Remove(((ListItem)arraylist1[i]));
}
ListBox2.SelectedIndex = -1;
}
else
{
}
/* This database call can be removed
SqlConnection con = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("select * from IT_1_BOILER_DESK_1_PARAMETERS where paramtext='" + Session["value"] + "'", con);
SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
dataAadpter.Fill(ds);
if (ds != null)
{
ListBox1.DataSource = ds.Tables[0];
ListBox1.DataTextField = "param";
//ListBox3.DataValueField = "param";
ListBox1.DataBind();
}*/

Invalid object name

The first one is function with query to access information about driver from table tblDDDDriver by using VehicleNumber which is one of the field or column name in tblDDDDriver.
The second one is what i coded to access that function.
Now when i click the button i shows the error Invalid object name 'tblDDDDriver'
public DataTable GetDriverByVehicalNumber(string VehicleNumber)
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security=True; Initial Catalog=tprojectDB;");
string sql = "select *from tblDDDDriver where VehicleNumber=#VehicleNumber";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("#VehicleNumber", VehicleNumber);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
private void button6_Click(object sender, EventArgs e)
{
DataTable dt = dc.GetDriverByVehicalNumber(txtvehicleno.Text);
if (dt.Rows.Count > 0)
{
txtlicenseno.Text = dt.Rows[0]["LicenseNumber"].ToString();
txtlicensecategory.Text = dt.Rows[0]["LicenseCategory"].ToString();
txtissuedate.Text = dt.Rows[0]["IssueDate"].ToString();
txtrenewdate.Text = dt.Rows[0]["RenewDate"].ToString();
txtfullname.Text = dt.Rows[0]["FullName"].ToString();
txtdob.Text = dt.Rows[0]["DOB"].ToString();
txtaddress.Text = dt.Rows[0]["Address"].ToString();
string gender = dt.Rows[0]["Gender"].ToString();
if (gender == "Male")
{
txtgender.Text = " MALE";
}
else
{
txtgender.Text = "FEMALE";
}
txtvehicleno.Text = dt.Rows[0]["VehicleNumber"].ToString();
txthealthstaus.Text = dt.Rows[0]["HealthStatus"].ToString();
txtdrivertype.Text = dt.Rows[0]["DriverType"].ToString();
Image img;
byte[] bytimg = (byte[])dt.Rows[0]["Image"];
//convert byte of imagedate to Image format
using (MemoryStream ms = new MemoryStream(bytimg, 0, bytimg.Length))
{
ms.Write(bytimg, 0, bytimg.Length);
img = Image.FromStream(ms, true);
pictureBox1.Image = img;
}
}
DataTable dd = dc.GetMaxDeathNo(Convert.ToDecimal(txtlicensenumber.Text));
if (dd.Rows.Count > 0)
{
txtdeathaccidentno.Text = dd.Rows[0]["DeathNumber"].ToString();
}
DataTable dM = dc.GetMaxMajorNo(Convert.ToDecimal(txtlicensenumber.Text));
if (dM.Rows.Count > 0)
{
txtmajoraccidentno.Text = dM.Rows[0]["MajorNumber"].ToString();
}
DataTable dm = dc.GetMaxMinorNo(Convert.ToDecimal(txtlicensenumber.Text));
if (dm.Rows.Count > 0)
{
txtminoraccidentno.Text = dm.Rows[0]["MinorNumber"].ToString();
}
DataTable dtrb = dc.GetTrafficRuleBroken(Convert.ToDecimal(txtlicensenumber.Text));
{
dataGridView1.DataSource = dtrb;
}
}

how to display comparison chart using ajax

I am trying to display ajax bar chart in my web page. But it is only displaying one value .
My db contains 3 columns(name, credit, debit) I want to display the credit debit values in chart. But the chart is only displaying one value. How can I modify the given below coding. Thank you.
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select Name from aTable";
DataTable dt = GetData(query);
ddlCountries.DataSource = dt;
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "Name";
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("Select", ""));
}
}
private DataTable GetData(string query, SqlParameter[] prms = null)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["demoConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
if (prms != null)
cmd.Parameters.AddRange(prms);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
string query = "select Name, Debit, Credit From aTable where Name=#Name";
SqlParameter[] prms = new SqlParameter[1];
prms[0] = new SqlParameter("#name", SqlDbType.NVarChar);
prms[0].Value = ddlCountries.SelectedItem.Value.ToString();
DataTable dt = GetData(query, prms);
string[] x = new string[dt.Rows.Count];
decimal[] y = new decimal[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
BarChart1.Series.Add(new AjaxControlToolkit.BarChartSeries { Data = y });
BarChart1.CategoriesAxis = string.Join(",", x);
BarChart1.ChartTitle = string.Format("{0} -RunTimeReportChart", ddlCountries.SelectedItem.Value);
if (x.Length > 3)
{
BarChart1.ChartWidth = (x.Length * 100).ToString();
}
BarChart1.Visible = ddlCountries.SelectedItem.Value != "";
}
Data Base:
Actual Output:
The given below chart is only displaying the name and debit value. I want to display the credit value also. Please help me.
Something Like :
decimal[] z = new decimal[dt.Rows.Count];
z[i] = Convert.ToInt32(dt.Rows[i][2]);
BarChart1.Series.Add(new AjaxControlToolkit.BarChartSeries { Data = z });

how to use repeater iteratively?

I have one repeater which displays the list of puja.i have also used one datalist to display the names of temples.whenever i click on search it displays only the last value of the repeater i.e it is taking only the last value of the repeater....my question is how to use loop inside a repeater???
here is my code behind how i used datalist and repeater:
protected void Page_Load(object sender, EventArgs e)
{
//lblnodatafound.Visible = true;
//lbldatafound.Visible = false;
if (Request.QueryString["search"] != null)
{
var dt = new DataTable("Data");
String source = Request.QueryString["search"];
var splitseparator = new string[] { " " };
String[] result = source.Split(splitseparator, StringSplitOptions.RemoveEmptyEntries);
foreach (String s in result)
{
if (IsAlphaNumeric(s) == true)
{
var dttemp = new DataTable();
dttemp = fnsearch(s);
dt.Merge(dttemp, true);
}
}
if (dt.Rows.Count != 0)
{
int count = dt.Rows.Count;
if (dt.Rows.Count > 0)
{
DataList1.DataSource = dt;
DataList1.DataBind();
lblnodatafound.Visible = false;
lbldatafound.Visible = true;
//added the repeater to display the list of puja
for (int i = 0; i < dt.Rows.Count; i++)
{
String id = dt.Rows[i]["id"].ToString();
string query = "select puja.id,puja.name as puja_name, mandir.name as mandir_name from puja,mandir where mandir.id= puja.with_mandir and puja.with_mandir = '" + id + "'";
conn.Open();
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataTable dt1 = new DataTable();
adp.Fill(dt1);
conn.Close();
if (dt1.Rows.Count > 0)
{
lblmandirpuja.Text = "Poja in " + dt1.Rows[0]["mandir_name"];
foreach (RepeaterItem repeatItem in Repeater1.Items){
Repeater1.DataSource = dt1;
Repeater1.DataBind();
}
}
}
}
}
else
{
lblnodatafound.Visible = true;
lbldatafound.Visible=false;
}
}
}
Try to change this line:
if (dt1.Rows.Count > 0)
for something like:
for(int i = 0; i < dt1.Rows.Count; i++)
And this one:
lblmandirpuja.Text = "Poja in " + dt1.Rows[0]["mandir_name"];
The 0 for the i:
lblmandirpuja.Text = "Poja in " + dt1.Rows[i]["mandir_name"];

Categories

Resources