question

Jramos avatar image
0 Votes"
Jramos asked NicoZhu-MSFT commented

Remove Datagrid row in Sql Server uwp c#

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)
             {
    
             }
windows-uwp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yep, I got your code, but I can't figure out what's problem that you want to ask.

0 Votes 0 ·
Jramos avatar image
0 Votes"
Jramos answered NicoZhu-MSFT commented

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

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You need to execute con.Execute("DELETE FROM TableName WHERE Id = ?", Id); then reload the table.

0 Votes 0 ·
 cmd.ExecuteNonQuery("DELETE FROM PetTabel Where  IdPet= ?", IdPet);

when I put the fine IdPet it indicates error.. Problem I have DataGrid (toolkit) where the user points out Item that they want to delete.

0 Votes 0 ·

Please share the error detail for us.

0 Votes 0 ·
Show more comments
Jramos avatar image
0 Votes"
Jramos answered NicoZhu-MSFT commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you mean you have solve this problem, if so please mark yourself answer.

0 Votes 0 ·
Jramos avatar image
0 Votes"
Jramos answered NicoZhu-MSFT commented

Hello

i'm changing the code.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok, if you have any updates, please post above comment.

0 Votes 0 ·
Jramos avatar image
0 Votes"
Jramos answered
 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"));
    
             }
    
    
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jramos avatar image
0 Votes"
Jramos answered NicoZhu-MSFT commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The better way is make Ischeck property for the data model, and bind it with checkbox ischeck property. you could traverse the collection and delete items that ischeck is true.

0 Votes 0 ·