Beispiel #1
0
        private void back_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection sqlConFill = new SqlConnection(conn);
            SqlCommand cmdFill = new SqlCommand();
            cmdFill.CommandText = "SELECT MAX(Cat_Id) FROM catagory_list";
            cmdFill.Parameters.AddWithValue("@CatagoryId", CatagoryIdTxt.Text);
            cmdFill.Connection = sqlConFill;
            sqlConFill.Open();
            int sr=0;
            SqlDataReader rd1 = cmdFill.ExecuteReader();
            if (rd1.HasRows)
            {
                rd1.Read();
                var outputParam = rd1[0];
                if (!(outputParam is DBNull))
                {
                    sr = Convert.ToInt32(rd1[0]);
                }
                else
                {
                    sr = 0;
                }
                rd1.Close();
            }
            sqlConFill.Close();
            if (sr > 0)
            {
                for (int i = 1; i <= sr; i++)
                {
                    SqlConnection sqlConFill2 = new SqlConnection(conn);
                    SqlCommand cmdFill2 = new SqlCommand();
                    cmdFill2.CommandText = "SELECT Cat_Name FROM catagory_list WHERE Cat_Id=@catId3";
                    cmdFill2.Parameters.AddWithValue("@catId3", Convert.ToString(i));
                    cmdFill2.Connection = sqlConFill2;
                    sqlConFill2.Open();
                    SqlDataReader rd3 = cmdFill2.ExecuteReader();
                    String cat = "";
                    if (rd3.HasRows)
                    {
                        rd3.Read();
                        cat = cat + rd3[0].ToString();
                        rd3.Close();
                        sqlConFill2.Close();
                        SqlConnection sqlUpdate = new SqlConnection(conn);
                        SqlCommand cmdUpdate = new SqlCommand();
                        cmdUpdate.CommandText = "UPDATE Foods SET CatagoryName=@CatagoryName WHERE Catagory_Id=@Catagory_Id";
                        cmdUpdate.Parameters.AddWithValue("@Catagory_Id", Convert.ToString(i));
                        cmdUpdate.Parameters.AddWithValue("@CatagoryName", cat);
                        cmdUpdate.Connection = sqlUpdate;
                        sqlUpdate.Open();
                        cmdUpdate.ExecuteNonQuery();
                        sqlUpdate.Close();
                    }
                }
            }

            //reordering item id
            SqlConnection sqlMaxSearch = new SqlConnection(conn);
            SqlCommand cmdMaxSearch = new SqlCommand();
            cmdMaxSearch.CommandText = "SELECT COUNT(*) FROM Foods";
            cmdMaxSearch.Connection = sqlMaxSearch;
            sqlMaxSearch.Open();
            int ct = 0;
            SqlDataReader rdmax = cmdMaxSearch.ExecuteReader();
            if (rdmax.HasRows)
            {
                rdmax.Read();
                var outputParam = rdmax[0];
                if (!(outputParam is DBNull))
                {
                    ct = Convert.ToInt32(rdmax[0]);
                }
                else
                {
                    ct = 0;
                }
                rdmax.Close();
            }
            sqlMaxSearch.Close();
            if (ct > 0)
            {
                SqlConnection sqlAllRow = new SqlConnection(conn);
                SqlCommand cmdAllRow = new SqlCommand();
                cmdAllRow.CommandText = "SELECT Food_Id FROM Foods";
                cmdAllRow.Connection = sqlAllRow;
                sqlAllRow.Open();
                SqlDataReader rdAllRow = cmdAllRow.ExecuteReader();
                String cat = "";
                int i = 1;
                if (rdAllRow.HasRows)
                {
                    while(rdAllRow.Read())
                    {
                        cat = rdAllRow[0].ToString();
                        SqlConnection sqlUpdateItem = new SqlConnection(conn);
                        SqlCommand cmdUpdateItem = new SqlCommand();
                        cmdUpdateItem.CommandText = "UPDATE Foods SET Food_Id=@foodIdUpdated WHERE Food_Id=@foodIdOld";
                        cmdUpdateItem.Parameters.AddWithValue("@foodIdUpdated", Convert.ToString(i));
                        cmdUpdateItem.Parameters.AddWithValue("@foodIdOld", cat);
                        cmdUpdateItem.Connection = sqlUpdateItem;
                        sqlUpdateItem.Open();
                        cmdUpdateItem.ExecuteNonQuery();
                        sqlUpdateItem.Close();
                        i++;
                    }
                    rdAllRow.Close();
                    sqlAllRow.Close();
                }
                sqlAllRow.Close();
            }

            Manager win2 = new Manager();
            win2.Show();
            this.Close();
        }
Beispiel #2
0
 private void back_Click(object sender, RoutedEventArgs e)
 {
     Manager win2 = new Manager();
     win2.Show();
     this.Close();
 }