public void SearchByRecepieType() { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString())) { SqlCommand query = new SqlCommand(@"exec Get_AllReceptsByType @TypeName", conn); query.Parameters.AddWithValue("@TypeName", selectedType); conn.Open(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query); DataTable dataTable = new DataTable(); sqlDataAdapter.Fill(dataTable); Recepies.Clear(); foreach (DataRow row in dataTable.Rows) { Recept r = new Recept { ReceptId = int.Parse(row[0].ToString()), UserId = int.Parse(row[1].ToString()), TypeId = int.Parse(row[2].ToString()), ReceptName = row[3].ToString(), PersonNumber = int.Parse(row[4].ToString()), Author = row[5].ToString(), ReceptText = row[6].ToString(), CreationDate = DateTime.Parse(row[7].ToString()), ReceptType = row[9].ToString(), }; if (LoginWindow.CurrentUser.UserId == r.UserId) { r.CanEdit = true; } if (LoginWindow.CurrentUser.Username == "Admin") { r.CanEdit = true; r.CanDelete = true; } using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString())) { SqlCommand query1 = new SqlCommand(@"exec Get_AllReceptsComponentsNumber @ReceptID = @Id;", conn1); query1.Parameters.AddWithValue("@Id", r.ReceptId); conn1.Open(); SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(query1); DataTable dataTable1 = new DataTable(); sqlDataAdapter1.Fill(dataTable1); foreach (DataRow row1 in dataTable1.Rows) { r.ComponentsNumber = int.Parse(row1[0].ToString()); } } Recepies.Add(r); } } }
public void SearchByRecepieComponent() { string[] words = components.Split(' '); Recepies.Clear(); UserHaveComponents.Clear(); MissingComponents.Clear(); foreach (string s in words) { if (s == "") { continue; } using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString())) { SqlCommand query1 = new SqlCommand(@"exec Get_AllComponentsByInput @ComponentName;", conn1); query1.Parameters.AddWithValue("@ComponentName", s); conn1.Open(); SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(query1); DataTable dataTable1 = new DataTable(); sqlDataAdapter1.Fill(dataTable1); foreach (DataRow row1 in dataTable1.Rows) { Components c = new Components { ComponentId = int.Parse(row1[0].ToString()), ReceptId = int.Parse(row1[1].ToString()), ComponentName = row1[2].ToString(), ComponentAmount = int.Parse(row1[3].ToString()) }; bool contains = false; foreach (Components item in UserHaveComponents) { if (item.ComponentName == c.ComponentName) { contains = true; break; } } if (!contains) { UserHaveComponents.Add(c); } } conn1.Close(); } using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString())) { SqlCommand query = new SqlCommand(@"exec Get_AllReceptsByComponents @Components", conn); query.Parameters.AddWithValue("@Components", s); conn.Open(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query); DataTable dataTable = new DataTable(); sqlDataAdapter.Fill(dataTable); foreach (DataRow row in dataTable.Rows) { Recept r = new Recept { ReceptId = int.Parse(row[0].ToString()), UserId = int.Parse(row[1].ToString()), TypeId = int.Parse(row[2].ToString()), ReceptName = row[3].ToString(), PersonNumber = int.Parse(row[4].ToString()), Author = row[5].ToString(), ReceptText = row[6].ToString(), CreationDate = DateTime.Parse(row[7].ToString()), ReceptType = row[9].ToString(), }; if (LoginWindow.CurrentUser.UserId == r.UserId) { r.CanEdit = true; } if (LoginWindow.CurrentUser.Username == "Admin") { r.CanEdit = true; r.CanDelete = true; } using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString())) { SqlCommand query1 = new SqlCommand(@"exec Get_AllReceptsComponentsNumber @ReceptID = @Id;", conn1); query1.Parameters.AddWithValue("@Id", r.ReceptId); conn1.Open(); SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(query1); DataTable dataTable1 = new DataTable(); sqlDataAdapter1.Fill(dataTable1); foreach (DataRow row1 in dataTable1.Rows) { r.ComponentsNumber = int.Parse(row1[0].ToString()); } } List <int> ids = new List <int>(); foreach (Recept i in Recepies) { ids.Add(i.ReceptId); } if (!ids.Contains(r.ReceptId)) { Recepies.Add(r); } } conn.Close(); } } }