I m trying to create dynamic menu using C# WinForm Project. My database structure is mentioned below. I can create the top menu ie "FILE", "EDIT", "SEARCH". But I cannot run the recursive function for the Child menu.
mysql> select * from dynamicmenu;
+------+-----------------+--------+-------+-----------+
| SLN | Child | Parent | Level | Reference |
+------+-----------------+--------+-------+-----------+
| 1 | FILE | NULL | 0 | NULL |
| 2 | EDIT | NULL | 0 | NULL |
| 3 | SEARCH | NULL | 0 | NULL |
| 4 | Session Manager | FILE | 1 | 1 |
| 5 | Connect To | FILE | 1 | 1 |
| 6 | DO | FILE | 2 | 5 |
| 7 | Copy | EDIT | 1 | 2 |
| 8 | Paste | EDIT | 1 | 2 |
| 9 | Find Text | SEARCH | 1 | 3 |
| 10 | Cut | EDIT | 2 | 7 |
| 11 | VMS | FILE | 3 | 6 |
+------+-----------------+--------+-------+-----------+
11 rows in set (0.00 sec)
The c# code is as follows.
mysqlConn conn;
private void Form1_Load(object sender, EventArgs e)
{
conn = new mysqlConn();
conn.OpenConnection();
MySqlCommand cmd = new MySqlCommand("Select * from dynamicmenu", conn.connection);
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["Parent"].ToString() == "")
{
menuStrip1.Items.Add(dr["Child"].ToString());
}
else if (dr["Parent"].ToString() != "")
{
AddSubMenu(new ToolStripMenuItem(dr["Child"].ToString()), dr["Parent"].ToString());
}
}
dr.Close();
conn.CloseConnection();
}
private void AddSubMenu(ToolStripMenuItem ChildItem, string ParentItem)
{
foreach (ToolStripMenuItem item in menuStrip1.Items)
{
if (ParentItem == item.Name)
{
item.DropDownItems.Add(ChildItem);
}
}
}
Please advise.
Use toString() instead of name.
private void AddSubMenu(ToolStripMenuItem ChildItem, string ParentItem)
{
foreach (ToolStripMenuItem item in menuStrip1.Items)
{
if (ParentItem == item.ToString())
{
item.DropDownItems.Add(ChildItem);
}
}
}
Related
I got some code which makes an SQL query and pastes the result into a datagridview. This works perfectly. The code be seen below:
private void FillInDataGrid()
{
string cn = ConfigurationManager.ConnectionStrings["Scratchpad"].ConnectionString;
SqlConnection myConnection = new SqlConnection(cn);
string sql =
"SELECT " +
" dbo.new.[column1], dbo.new.[column2], dbo.new.[column3], dbo.new.[column4] from dbo.new" +
"except " +
"select dbo.old.[column1], dbo.old.[column2], dbo.old.[column3], dbo.old.[column4] from dbo.old"
;
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, myConnection);
DataSet ds = new DataSet();
myConnection.Open();
dataadapter.Fill(ds, "Authors_table");
myConnection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Authors_table";
}
This code displays the values of the row from the new table if it doenst exists in the old table. Now I want to color the cells where they differ. So for example if you have those two tables:
new table:
--------------------------------------
| column1 | column2| column3| column4|
--------------------------------------
| 1 | 1 | 1 | 2 |
--------------------------------------
| 2 | 2 | 3 | 2 |
--------------------------------------
| 6 | 7 | 9 | 8 |
--------------------------------------
| 7 | 5 | 34 | 22 |
--------------------------------------
| 3 | 4 | 3 | 3 |
--------------------------------------
old table:
--------------------------------------
| column1 | column2| column3| column4|
--------------------------------------
| 6 | 7 | 9 | 8 |
--------------------------------------
| 7 | 5 | 34 | 22 |
--------------------------------------
| 1 | 1 | 1 | 1 |
--------------------------------------
| 2 | 2 | 2 | 2 |
--------------------------------------
| 3 | 3 | 3 | 3 |
--------------------------------------
So this example would show:
--------------------------------------
| column1 | column2| column3| column4|
--------------------------------------
| 1 | 1 | 1 | *2* |
--------------------------------------
| 2 | 2 | *3* | *2* |
--------------------------------------
| 3 | *4* | *3* | *3* |
--------------------------------------
Where the * 2 *, * 3 * and * 4 * would show a color in the datagridview.
Thanks in advance for your help.
I have a query that needs to retrieve 3 fields:
| MaintenanceID | MaintenanceIDCount | StatusID |
| 1 | 2 | -1 |
| 3 | 2 | -1 |
The field MaintenanceIDCount (like the name says), is the count of MaintenanceID column.
My basic query expression is above:
var result = from m in Maintenance
select new
{
m.MaintenanceID,
m.StatusID
}
The result of this query is:
| MaintenanceID | StatusID |
| 1 | -1 |
| 1 | -1 |
| 3 | -1 |
| 3 | -1 |
How can I group and mount my query to retrieve a column with the MaintenanceID column count?
Some tips?
from m in Maintenance
group m by new { m.MaintenanceID, m.StatusID } into g
select new {
g.Key.MaintenanceID,
g.Key.StatusID,
MaintenanceIDCount = g.Count()
}
I have two tables called tableFileTemp2 and tableFile. I need a distinct list of computers merging the path and file columns into tableFile tables path column...
tableFileTemp2 //source
+-------+----------+--------+-------+
| host | path | date | file |
+-------+----------+--------+-------+
| comp1 | c:\ | xydate | x.exe |
| comp1 | c:\Temp\ | xydate | x.exe |
| comp2 | c:\win\ | xydate | y.exe |
| comp2 | c:\win\ | xydate | z.exe |
+-------+----------+--------+-------+
tableFile //this is the result, apparently the first path+file is being duplicated for each machine
+-------+---------------------------------------------------+--------+------+
| host | path | date | file |
+-------+---------------------------------------------------+--------+------+
| comp1 | c:\x.exe<br>c:\x.exe<br>c:\Temp\x.exe<br> | xydate | null |
| comp2 | c:\win\y.exe<br>c:\win\y.exe<br>c:\win\z.exe<br> | xydate | null |
+-------+---------------------------------------------------+--------+------+
tableFile //expected result
+-------+----------------------------------+--------+------+
| host | path | date | file |
+-------+----------------------------------+--------+------+
| comp1 | c:\x.exe<br>c:\Temp\x.exe<br> | xydate | null |
| comp2 | c:\win\y.exe<br>c:\win\z.exe<br> | xydate | null |
+-------+----------------------------------+--------+------+
my code:
for (int s = 0; s < tableFileTemp2.Rows.Count; s++)
{
if (tableFile.Rows.Count != 0)
{
for (int t = 0; t < tableFile.Rows.Count; t++)
{
if (string.Equals(tableFile.Rows[t][0].ToString(), tableFileTemp2.Rows[s][0].ToString(), StringComparison.OrdinalIgnoreCase))
{
tableFile.Rows[t][1] = tableFile.Rows[t][1].ToString() + tableFileTemp2.Rows[s][1].ToString() + tableFileTemp2.Rows[s][3].ToString() + "<br>";
break;
}
else if (t == (tableFile.Rows.Count - 1))
{
tableFile.Rows.Add(tableFileTemp2.Rows[s][0].ToString(), (tableFileTemp2.Rows[s][1].ToString() + tableFileTemp2.Rows[s][3].ToString() + "<br>"), tableFileTemp2.Rows[s][2], null);
}
}
}
else
{
tableFile.Rows.Add(tableFileTemp2.Rows[s][0].ToString(), (tableFileTemp2.Rows[s][1].ToString() + tableFileTemp2.Rows[s][3].ToString() + "<br>"), tableFileTemp2.Rows[s][2], null);
}
}
As you say in your comment that you still have the original CSV that the data came from. We can do this with LINQ.
+-------+----------+--------+-------+
| host | path | date | file |
+-------+----------+--------+-------+
| comp1 | c:\ | xydate | x.exe |
| comp1 | c:\Temp\ | xydate | x.exe |
| comp2 | c:\win\ | xydate | y.exe |
| comp2 | c:\win\ | xydate | z.exe |
+-------+----------+--------+-------+
Then the code looks like.
var results =
from thing in
(from line in tableFileCSV.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None)
let row = line.Split(',')
select new
{
Host = row[0],
Path = row[1] + row[3] + "</br>",
Date = row[2],
File = row[3] // <- Are you sure you want this to be null and not the file value?
})
group new { thing.Path, thing.Date, thing.File } by new { thing.Host } into g
select new
{
Host = g.Key.Host,
Path = g.Select(i => i.Path).Aggregate((a, b) => a + b),
Date = g.Select(i => i.Date).FirstOrDefault(),
File = "File",
};
// If you want to get a look at it.
foreach (var item in results)
{
Console.WriteLine("{0} {1} {2} {3}", item.Host, item.Path, item.Date, item.File);
}
Where the tableFileCSV is your original data in a CSV.
This can then be passed into a datatable.
var dt = new DataTable();
dt.Columns.Add("Host");
dt.Columns.Add("Path");
dt.Columns.Add("Date");
dt.Columns.Add("File");
foreach (var item in results)
{
dt.Rows.Add(item.Host,item.Path,item.Date,item.File);
}
I have two tables to treat in a query for getting the product in hand.
ProdBiscuit
+----+-----+-------+---------------+
| ID | QTY |StockID| Name |
+----+-----+-------+---------------+
| 1 | 100 | 1 | Bis Chocolat |
+----+-----+-------+---------------+
| 2 | 120 | 2 | Bis Moutarde |
+----+-----+-------+---------------+
| 3 | 100 | 3 | Bis Epice |
+----+-----+-------+---------------+
StockData
+----+-----+-------+------+----+
| ID | QTY |ProdID |Status|Type|
+----+-----+-------+------+----+
| 1 | 100 | 1 | 0 | 3 |
+----+-----+-------+------+----+
| 2 | 120 | 2 | 0 | 3 |
+----+-----+-------+------+----+
| 3 | 100 | 3 | 0 | 3 |
+----+-----+-------+------+----
| 4 | 200 | 1 | 0 | 4 |
+----+-----+-------+------+----+
| 5 | 200 | 2 | 0 | 4 |
+----+-----+-------+------+----+
| 6 | 48 | 1 | 0 | 2 |
+----+-----+-------+------+----+
| 7 | 96 | 2 | 4 | 3 |
+----+-----+-------+------+----+
| 8 | 200 | 1 | 4 | 4
+----+-----+-------+------+----+
ProdBiscuit ID is relational with StockData.ProdID
ProdBiscuit.StockId is relational with StockData.ID
StockData.Status = 0 => Entry in stock, > 0 =>Left the stock
Type means 2=>Macaron, 3=>Biscuit, 4=>Ganache
ProdID => ID of ProdBiscuit
StockID-> ID of StockData
With the method below I get this
+--------------+--------+---------+
|Name |QTY PROD|QTY REST |
+--------------+--------+---------+
|Bis Chocolat | 100 | 100 |
+--------------+--------+---------+
|Bis Moutarde | 120 | 24 |
+--------------+--------+---------+
|Bis Epice | 100 | 100 |
+--------------+--------+---------+
Result is correct but the query seems to me to be incorrect.
Because in WHERE clause query search the rows where status > 0
However I get the correct result??
Here is my code :
private void RefreshProd()
{
Int16 ProdQty;
Int16 UsedQty;
DateLst.Items.Clear();
ProdLst.Items.Clear();
QtyLst.Items.Clear();
NomLst.Items.Clear();
prodidLst.Items.Clear();
using (MySqlConnection conn = new MySqlConnection(PublicVariables.cs))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT tb.id, tb.qty,tb.nom, tb.proddate, IF(status > 0 AND tb.id = sd.prodid, sd.quantite, 0) AS rq FROM StockData AS sd, " + TableName + " AS tb WHERE sd.matcuisine = 3 AND sd.status > 0", conn))
{
conn.Open();
try
{
MySqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
DateLst.Items.Add(Convert.ToDateTime(r["proddate"]).ToString("d"));
ProdQty = Convert.ToInt16(r["qty"]);
UsedQty = Convert.ToInt16(r["rq"]);
ProdLst.Items.Add(r["qty"]).ToString();
NomLst.Items.Add(r["nom"]);
prodidLst.Items.Add(r["id"]).ToString();
QtyLst.Items.Add((ProdQty-UsedQty).ToString());
}
}
catch (MySqlException e)
{
MessageBox.Show("MYSQL ERROR--> " + e.ToString());
}
}
}
}
I could not find the rub.
The thing that's astonishing me that the query seems to me to be faulty but gives a correct result.
In where clause I put a condition that the search must be "sd.status>0" but it seems to me it's fetching the status = ZERO conditions too.(Perhaps I am missing something technical in MYSQL WHERE clause)
How ever bu doing my premenade overthestackflow I learnt that UNION and JOIN is more powerfull then the method that I use.
Is there a more efficient way to write my query by JOIN oro UNION? I will appreciate your help. Because I couldn't find way to resolve it by this way.
this query?
SELECT
tb.id
, tb.qty
, tb.nom
, tb.proddate
, sd.quantity as rq
FROM
StockData AS sd
INNER JOIN " + TableName + " AS tb
On tb.id = sd.prodid
WHERE
sd.matcuisine = 3
AND sd.status > 0
Im trying to read records from a database using Entity Framework , which I have not used before.
Im trying to run the load rule method:
class Rule
{
private STRATCODE stratCode;
private STRATRULEDEF stratRuleDef;
AREEntities AREDb = new AREEntities();
public Rule(STRATRULEDEF StratRuleDef, STRATCODE StratCode)
{
this.stratRuleDef = StratRuleDef;
this.stratCode = StratCode;
}
public void LoadRule()
{
var query = from stratCode in AREDb.STRATCODES
where stratCode.CODEVALUE == "49300"
select stratCode;
//StratCodeRepository StratCodeRepository = new StratCodeRepository(new AREEntities());
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"C:\Users\user\Desktop\NewText.txt",true))
{
foreach (STRATCODE row in query)
{
file.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}", stratCode.STRATRULEKEY, stratCode.CODETYPE, stratCode.CODEVALUE, stratCode.SYSTEMID, stratCode.CODESETKEY, stratCode.PAYMENTYEAR);
}
}
}
My problem is that everytime I run it, it returns the right number of records but all of the data in the text file is empty and I can find no clues in my output log.
0 | | | | |
0 | | | | |
0 | | | | |
0 | | | | |
0 | | | | |
Any ideas? I dont know how to even start with no clues
Your write is not using the foreach variable:
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"C:\Users\stephen.carmody\Desktop\NewText.txt", true))
{
foreach (var row in query)
{
file.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}",
row.STRATRULEKEY,
row.CODETYPE,
row.CODEVALUE,
row.SYSTEMID,
row.CODESETKEY,
row.PAYMENTYEAR);
}
}