Why when i write ItemListView it gives me error? - c#

I'm working on a job about checking in and out of a school. And when I write ListViewItem this always gives an error:
Here is my code:
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Alunos WHERE EntradaSaida = 0", conn);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
ListViewItem item = new ListViewItem(dr["ID"].ToString());
item.SubItems.Add(dr["Nome"].ToString());
item.SubItems.Add(dr["Sobrenome"].ToString());
item.SubItems.Add(dr["TurmaID"].ToString());
item.SubItems.Add(dr["Data"].ToString());
lvEntrada.Items.Add(item);
}
if (lvEntrada.SelectedItems.Count > 0)
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("UPDATE Alunos SET ID =#ID EntradaSaida =#Entrada, Data = #Data ", conn))
{
cmd.Parameters.AddWithValue("#ID", ID);
cmd.Parameters.AddWithValue("#Data", DateTime.Now);
cmd.Parameters.AddWithValue("#Entrada", 1);
int rows = cmd.ExecuteNonQuery();
}
}

Add
using System.Windows.Forms;
at the top of your cs file.
Or you know what else.... don't pass in a parameter to your listView...
ListViewItem item = new ListViewItem();
item.SubItems.Add(dr["ID"].ToString());
item.SubItems.Add(dr["Nome"].ToString());
item.SubItems.Add(dr["Sobrenome"].ToString());
item.SubItems.Add(dr["TurmaID"].ToString());
item.SubItems.Add(dr["Data"].ToString());
lvEntrada.Items.Add(item);
What is lvEntrada? does it have a property named Items?

Related

How to get checkedboxlist item Id from C# winform

How i can get id when i click on item of checkedboxlist. Currently i am getting string name of the Item but i want to get id when i click on text item in checkedboxlist ?
Here is my code:
private void BindChackBox2()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager
.ConnectionStrings["Conec"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT Id, Task FROM Mytodo_Task
Where Status='Ongoing' And Username='" + Login.recuser + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
checkedListBoxongoing.Items.Add(dt.Rows[i]["Task"].ToString() +" "+ dt.Rows[i];
["Id"].ToString());
}
}
catch { }
}

Highlight specific items in listbox based on their status in database

I have a table in a database with ID, name and gender columns. I populate the listbox like this:
public void fill_listbox()
{
lbProjects.Items.Clear();
con.Open();
cmd = new SqlCommand("SELECT name FROM Table1", con);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
lbProjects.Items.Add(dr["name"].ToString());
}
con.Close();
}
Is it possible to highlight (not select) all the items that have specific gender in the listbox?
For example change the background color/font-weight/font color for all items that have "male" as gender in the database.
If it is not possible using the listbox, what another component I can use?
Solved it by using listview instead.
public void fill_listbox()
{
listView1.Columns.Add("", -2, HorizontalAlignment.Left);
listView1.Items.Clear();
con.Open();
cmd = new SqlCommand("SELECT name,gender FROM Table1", con);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
ListViewItem lvi = new ListViewItem();
if (dr["gender"].ToString() == "male")
{
lvi.Text = dr["name"].ToString();
lvi.ForeColor = Color.Blue;
listView1.Items.Add(lvi);
}
else {
lvi.Text = dr["name"].ToString();
lvi.ForeColor = Color.Pink;
listView1.Items.Add(lvi);
}
}
con.Close();
}

Add All Option To Top of Bound Combobox

I need to add All as a selectable choice to the top of a combobox on winform. I have tried both of the codes below, which one does not throw an error but the value is not added:
public void GetData()
{
using (SqlConnection conn = new SqlConnection(SqlConnection))
{
SqlDataAdapter da = new SqlDataAdapter("select [empname] from [Testdb].[dbo].[Test] order by [empname] Asc", conn);
conn.Open();
da.Fill(ds, "Test");
cboemployees.Items.Insert(0, "All");
cboemployees.DisplayMember = "empname";
cboemployees.DataSource = ds.Tables["Test"];
}
}
Now this syntax throws an error of
error column 'empname' does not belong to table
public void GetData()
{
using (SqlConnection conn = new SqlConnection(SqlConnection))
{
SqlDataAdapter da = new SqlDataAdapter("select [empname] from [Testdb].[dbo].[Test] order by [empname] Asc", conn);
conn.Open();
da.Fill(ds, "Test");
DataRow row = dt.NewRow();
row["empname"] = "All";
dt.Rows.InsertAt(row, -1);
cboemployees.DataSource = dt;
}
}
What is the proper way of adding a selectable item to the top of a bound combobox?
Add the row to your table instead.
SqlDataAdapter da = new SqlDataAdapter("select [empname] from [Testdb].[dbo].[Test] order by [empname] Asc", conn);
conn.Open();
da.Fill(ds, "Test");
// Add the row for All
var allRow = ds.Tables["Test"].NewRow();
allRow[0] = "All";
ds.Tables["Test"].Rows.InsertAt(allRow, 0);
cboemployees.DisplayMember = "empname";
cboemployees.DataSource = ds.Tables["Test"];

How to Display Warning message if quantity is less than 5 items

I'm creating an inventory system which should display the products that needs to be restock. If the quantity of the product is only 5 it should display a message that says you need to restock now. But I don't know how to do that because I'm creating an Inventory System only and not Sales and Inventory so how should I deduct it if I don't have sales system? I'm sorry, hope u guys understand.
Here is my code:
public void all()
{
SqlConnection MySqlConnection;
DataTable p_table = new DataTable();
MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlConnection.Open();
SqlCommand command1 = new SqlCommand("Select * from inventory", MySqlConnection);
//Clear the datatable to prevent duplicate generation of data in gridview.
p_table.Clear();
SqlDataAdapter m_da = new SqlDataAdapter("Select * from inventory", MySqlConnection);
//DataSet ds = new DataSet();
//DataTable dtable = ds.Tables["empinfo1"];
m_da.Fill(p_table);
// Clear the ListView control
//listView3.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < p_table.Rows.Count; i++)
{
DataRow drow = p_table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
//(drow["bnum"].ToString());
ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
lvi.SubItems.Add(drow["pname"].ToString());
lvi.SubItems.Add(drow["descr"].ToString());
lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
//lvi.SubItems.Add(drow["exp"].ToString());
lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
lvi.SubItems.Add(drow["qt"].ToString());
// Add the list items to the ListView
listView2.Items.Add(lvi);
}
}
}
edited code:
public void stock()
{
SqlConnection MySqlConnection;
DataTable p_table = new DataTable();
MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlConnection.Open();
SqlCommand command1 = new SqlCommand("Select pname from inventory where qt < 5", MySqlConnection);
//Clear the datatable to prevent duplicate generation of data in gridview.
p_table.Clear();
SqlDataAdapter m_da = new SqlDataAdapter("Select pname from inventory where qt < 5", MySqlConnection);
m_da.Fill(p_table);
SqlDataReader reader;
reader = command1.ExecuteReader();
StringBuilder productNames = new StringBuilder();
while (reader.Read())
{
productNames.Append(reader["pname"].ToString() + Environment.NewLine);
}
MySqlConnection.Close();
MessageBox.Show("There are products that needs restocking, check to restock now." + productNames);
// Display items in the ListView control
for (int i = 0; i < p_table.Rows.Count; i++)
{
DataRow drow = p_table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
//ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
lvi.SubItems.Add(drow["pname"].ToString());
lvi.SubItems.Add(drow["descr"].ToString());
lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
//lvi.SubItems.Add(drow["exp"].ToString());
lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
lvi.SubItems.Add(drow["qt"].ToString());
lvi.SubItems.Add(drow["interval"].ToString());
// Add the list items to the ListView
listView4.Items.Add(lvi);
}
}
}
Try This:
void DisplayLowQuantityItems()
{
MySqlConnection con = new MySqlConnection("Data Source=christina\\sqlexpress;
Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlCommand command = new MySqlCommand("Select pname from inventory
where qt < 5", con);
con.Open();
MySqlDataReader reader = commad.ExecuteReader();
StringBuilder productNames= new StringBuilder();
while(reader.Read())
{
productNames.Append(reader["pname"].ToString()+Environment.NewLine);
}
con.Close();
MessageBox.Show("Following Products quantity is lessthan 5\n"+productNames);
}
Solution 2:
edited code:
public void stock()
{
MySqlConnection con;
DataTable p_table = new DataTable();
con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial
Catalog=cafe_inventory;User ID=sa;Password=tina;");
con.Open();
MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
qt < 5", con);
//Clear the datatable to prevent duplicate generation of data in gridview.
p_table.Clear();
MySqlDataAdapter m_da = new MySqlDataAdapter("Select * from inventory where
qt < 5", con);
m_da.Fill(p_table);
MySqlDataReader reader;
reader = command1.ExecuteReader();
StringBuilder productNames = new StringBuilder();
while (reader.Read())
{
productNames.Append(reader["pname"].ToString() + Environment.NewLine);
}
con.Close();
MessageBox.Show("There are products that needs restocking,
check to restock now." + productNames);
// Display items in the ListView control
for (int i = 0; i < p_table.Rows.Count; i++)
{
DataRow drow = p_table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
//ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
lvi.SubItems.Add(drow["pname"].ToString());
lvi.SubItems.Add(drow["descr"].ToString());
lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
//lvi.SubItems.Add(drow["exp"].ToString());
lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
lvi.SubItems.Add(drow["qt"].ToString());
lvi.SubItems.Add(drow["interval"].ToString());
// Add the list items to the ListView
listView4.Items.Add(lvi);
}
}
}
another way of checking your data
DataTable p_table = new DataTable();
con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial
Catalog=cafe_inventory;User ID=sa;Password=tina;");
con.Open();
MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
qt < 5", con);
p_table.load(command1.ExecuteReader());

Not able to read a DataTableReader inside another DataTableReader

Friends expecting ur suggetions to the below code. Instead of taking dtr1( DatatableReader) it is taking outside loop dtr(DataTableReader) Table.
protected void GetStudReport(Object o, EventArgs e)
{
if (mycon.State != ConnectionState.Open)
{
List<string> lstQstn = new List<string>();
mycon.Open();
cmd = new MySqlCommand("SELECT * from scord_mark_table where stu_ID='" + drpDnSearch3.SelectedValue + "'", mycon);
MySqlDataReader rdr1=cmd.ExecuteReader();
DataSet ds=new DataSet();
DataTable dtScrTbl=new DataTable();
dtScrTbl.Load(rdr1);
ds.Tables.Add(dtScrTbl);
rdr1.Close();
cmd = null;
int i = 0;
Dictionary<string, string> dctSub = new Dictionary<string, string>();
**using (DataTableReader dtr = ds.CreateDataReader())**
{
while (dtr.Read())
{
lstQstn.Add(dtr["test_id"].ToString());
while (i <= lstQstn.Count())
{
MySqlCommand cmd2 = new MySqlCommand("SELECT test_id,subject_id from qution_no_table where test_id='" + lstQstn[i].ToString() + "'", mycon);
MySqlDataReader rdr2 = cmd2.ExecuteReader();
DataTable dtQsNoTbl = new DataTable();
dtQsNoTbl.Load(rdr2);
ds.Tables.Add(dtQsNoTbl);
**using (DataTableReader dtr1 = ds.CreateDataReader())**
{
while (dtr1.Read())
{
dctSub.Add(dtr1["test_id"].ToString(), dtr1["subject_id"].ToString()); // **here it is taking table scord_mark_table instead of dtr1's qution_no_table**
}
rdr2.Close();
break;
}
}
//cmd2 = null;
i++;
}
}
No, it's not - it's returning a new DataTableReader hooked to ALL of the existing tables (including the one you created before the first loop).
Try specifying the DataTable you want the readers attached to:
using (DataTableReader dtr = ds.CreateDataReader(dtScrTbl))
and
using (DataTableReader dtr1 = ds.CreateDataReader(dtQsNoTbl))

Categories

Resources