i have this code and got error with Object reference not set to an instance of an object.
private void updateRecordToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem list in lvViewEmp.Items)
{
Employee ss = new Employee();
ss.Show();
frmEmp.txtEmpID.Text = lvViewEmp.SelectedItems[0].Text;
}
}
this is the code on getting my data.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyConnection"];
string MyConnection = conSettings.ConnectionString;
con = new SqlConnection(MyConnection);
cmd = con.CreateCommand();
cmd.CommandText = "Select * From tbl_emp_info";
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
dt = new DataTable();
sda.Fill(ds);
sda.Fill(dt);
if (con.State != ConnectionState.Open)
{
con.Open();
{
lvViewEmp.View = View.Details;
foreach (DataColumn c in ds.Tables[0].Columns)
{
ColumnHeader h = new ColumnHeader();
h.Text = c.ColumnName;
h.Width = 90;
lvViewEmp.Columns.Add(h);
}
string[] str = new string[ds.Tables[0].Columns.Count];
foreach (DataRow rr in dt.Rows)
{
for (int col = 0; col <= ds.Tables[0].Columns.Count - 1; col++)
{
str[col] = rr[col].ToString();
}
ListViewItem ii;
ii = new ListViewItem(str);
lvViewEmp.Items.Add(ii);
lvViewEmp.GridLines = true;
lvViewEmp.FullRowSelect = true;
con.Close();
}
}
}
how can i pass all data from the list view through textboxes on other form?
Related
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;
}
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();
}*/
please help me I stuck in datagridview in c# winforms where my sql table looks like below
empcode-varchar(50)
fullname-varchar(50)
month-date
branch-varchar(50)
designation-varchar(50)
id-varchar(50)
accountno-nvarchar(50)
paymenttype-nvarchar(50)
basicsal-int
ca-int
hra-int
sa-int
totalsalary-int
allowanceid-int(IDENTITY COLUMN)
remark-nvarchar(50)
For this table i took two buttons one is viewdata and another is delete
my view data coding part working fine and is shown below:-
SqlDataAdapter da = new SqlDataAdapter("select * from allowance", cn);
dt = new System.Data.DataTable();
da.Fill(dt);
dg2.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dg2.Rows.Add();
dg2.Rows[n].Cells[0].Value = false;
dg2.Rows[n].Cells[1].Value = item["empcode"].ToString();
dg2.Rows[n].Cells[2].Value = item["fullname"].ToString();
dg2.Rows[n].Cells[3].Value = item["month"].ToString();
dg2.Rows[n].Cells[4].Value = item["branch"].ToString();
dg2.Rows[n].Cells[5].Value = item["designation"].ToString();
dg2.Rows[n].Cells[6].Value = item["id"].ToString();
dg2.Rows[n].Cells[7].Value = item["accountno"].ToString();
dg2.Rows[n].Cells[8].Value = item["paymenttype"].ToString();
dg2.Rows[n].Cells[9].Value = item["basicsal"].ToString();
dg2.Rows[n].Cells[10].Value = item["ca"].ToString();
dg2.Rows[n].Cells[11].Value = item["hra"].ToString();
dg2.Rows[n].Cells[12].Value = item["sa"].ToString();
dg2.Rows[n].Cells[13].Value = item["totalsalary"].ToString();
dg2.Rows[n].Cells[14].Value = item["allowanceid"].ToString();
dg2.Rows[n].Cells[15].Value = item["remark"].ToString();
}
and another button "delete" code is looks like this way:-
foreach (DataGridViewRow itemRow in dg2.Rows)
{
if (bool.Parse(itemRow.Cells[14].Value.ToString()))
{
da = new SqlDataAdapter("DELETE FROM allowance WHERE allowanceid = '" + itemRow.Cells[14].Value.ToString() + "'", cn);
DataTable bb = new DataTable();
da.Fill(bb);
}
}
MessageBox.Show("SuccessFully DELETED.....!");
foreach (DataGridViewRow itemRow in dg2.Rows)
{
if (!itemRow.IsNewRow)
{
if ((bool)itemRow.Cells[0].EditedFormattedValue)
{
da = new SqlDataAdapter("DELETE FROM allowance WHERE allowanceid = " + Convert.ToInt32(itemRow.Cells[14].Value) + "", cn);
DataTable bb = new DataTable();
da.Fill(bb);
try this
foreach (DataGridViewRow itemRow in dg2.Rows)
{
if (bool.Parse(Convert.Tostring(itemRow.Cells[14].Value)))
{
da = new SqlDataAdapter("DELETE FROM allowance WHERE allowanceid = '" + Convert.Tostring(itemRow.Cells[14].Value) + "'", cn);
DataTable bb = new DataTable();
da.Fill(bb);
}
}
MessageBox.Show("SuccessFully DELETED.....!");
wire this code in DaataError Event
private void DGData_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{
if ((anError.Exception) is ConstraintException)
{
DataGridView grd1 = (DataGridView)sender;
grd1.Rows[anError.RowIndex].ErrorText = "an error";
grd1.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";
anError.ThrowException = false;
}
}
}
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 });
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"];