Share via

Remove Datagrid row in Sql Server uwp c#

Javier R 211 Reputation points
2021-06-14T19:49:59.117+00:00

Remove Datagrid row in Sql Server uwp c#
const string PetDeleteQuery = "Delete FROM PetTable where idPet = 1";
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();

                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = PetDeleteQuery;


                            cmd.ExecuteNonQuery();
                            conn.Close();
                        }

                    }

                }


            }
            catch(Exception ex)
            {

            }
Developer technologies | Universal Windows Platform (UWP)

5 answers

Sort by: Most helpful
  1. Javier R 211 Reputation points
    2021-11-10T16:44:00.133+00:00

    how can I delete the row in dataGrid using a checkbox to choose the row to delete and then press the delete button

    Was this answer helpful?


  2. Javier R 211 Reputation points
    2021-07-13T17:53:26.547+00:00
    public async void bind()
            {
    
                 pets = new ObservableCollection<PetTable>();
                try
                {
                    const string GetPetTableQuery = "SELECT * FROM  PetTable";
    
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        await conn.OpenAsync();
    
                        if (conn.State == System.Data.ConnectionState.Open)
                        {
                            using (SqlCommand cmd = new SqlCommand(GetPetTableQuery, conn))
                            {
                                using (SqlDataReader reader = cmd.ExecuteReader())
                                {
                                    if (reader.HasRows)
                                    {
                                        while (reader.Read())
                                        {
                                            PetTable pet = new PetTable();
    
                                            BitmapImage map = new BitmapImage();
                                            byte[] bytes = (byte[])reader["Picture"];
                                            var stream = new InMemoryRandomAccessStream();
                                            await stream.WriteAsync(bytes.AsBuffer());
                                            stream.Seek(0);
                                            await map.SetSourceAsync(stream);
                                            pet.Picture = map;
    
    
    
    
                                            pet.PetType = (string)reader["Type"];
                                            pet.DuoDate = (DateTime)reader["Date"];
                                            pet.PetName = (string)reader["Name"];
                                            pet.PetRace = (string)reader["Race"];
                                            pet.PetColor = (string)reader["Color"];
                                            pet.PetChip = (string)reader["Chip"];
                                            pet.PetGender = (string)reader["Gender"];
                                            pet.PetSterile = (string)reader["Sterile"];
    
                                            pets.Add(pet);
    
    
    
                                        }
    
                                    }
    
    
                                }
    
                            }
                        }
    
    
                    }
    
    
                }
                catch (Exception ex)
                {
                    var Msg = new MessageDialog("Exeception:" + ex.Message);
                    Msg.Commands.Add(new UICommand("Close"));
    
                }
    
    
            }
    

    Was this answer helpful?

    0 comments No comments

  3. Javier R 211 Reputation points
    2021-07-09T18:05:22.627+00:00

    Hello

    i'm changing the code.

    Was this answer helpful?


  4. Javier R 211 Reputation points
    2021-07-01T18:29:23.497+00:00

    I've changed the code I'm running to display the data for the datagrid

    Was this answer helpful?


  5. Javier R 211 Reputation points
    2021-06-16T18:01:57.247+00:00

    I have a DataGrid but when I point the line I can not delete the record from delete button. what I want to know is how to delete the record from the table when I choose it to be deleted the line

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.