question

Saravanaavigneshsana-2837 avatar image
0 Votes"
Saravanaavigneshsana-2837 asked JackJJun-MSFT commented

datatable.defaultview.rowfilter is not working repeatedly when calling my function in c#

 Button click {
                       SQL Query1 ..
                       Getdata( Query1);
                    }
    
  bool Getdat(string Query)
       {
         using (SqlConnection connection = new SqlConnection(connetionString))
             {
                    
                     connection.Open();
                     SqlDataReader rdr;
                     SqlCommand cmd = new SqlCommand(Query, connection);
                     cmd.CommandType = CommandType.StoredProcedure;
    
              
                     rdr = cmd.ExecuteReader();
    
                        
                     dtvehiclegetdat.Reset();
                                  
                     dtvehiclegetdat.Load(rdr);
                      
                     dtcbosearch_vehicle = dtvehiclegetdat.DefaultView.ToTable(true, "Column1", "Column2");
    
       return true;
                 }

"dtvehiclegetdat.DefaultView.ToTable(true, "Column1", "Column2"); "it is filter correct order in first time, but next time it is passing empty rows to datable

dotnet-csharp
· 6
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.


Did you check if dtvehiclegetdat always contains rows after executing Load(rdr)?


0 Votes 0 ·

yes ,it had rows and columns

0 Votes 0 ·

@Saravanaavigneshsana-2837, based on my test, I find that the code you provided is almost correct. Then, as Viorel-1 asked, we need to check if dtvehiclegetdat is null. Could you provide some code about the dtvehiclegetdat datatable?

You could try the following code to use a new datatable to check if it works for you.


        DataTable dtvehiclegetdat = new DataTable();

         dtvehiclegetdat.Reset();

         dtvehiclegetdat.Load(rdr);

         var dtcbosearch_vehicle = dtvehiclegetdat.DefaultView.ToTable(true, "Name", "Age");

0 Votes 0 ·
               rdr = cmd.ExecuteReader();

                 dtgetvehicleunit.Reset();
                 dtvehiclegetdat.Reset();
                 GlobalVariables.dtcbosearch_vehicle = null; // here used global public static datatable so unable to reset

                 //dtvehicleunitgetdata.Columns.Add(" ", typeof(int));
                 dtmenugetdata.Columns.Add("Sno", typeof(int));
                 dtmenugetdata.Columns["Sno"].AutoIncrement = true;
                 dtmenugetdata.Columns["Sno"].AutoIncrementSeed = 1;
                 dtmenugetdata.Columns["Sno"].AutoIncrementStep = 1;
                 dtvehiclegetdat.Load(rdr);...........................................................................................................................................2
                 //here take vehicle+unit global for all form
 //datatable// GlobalVariables.dtcbosearch_vehicle = dtvehiclegetdat.DefaultView.ToTable(true, "vehicleno", "vehicletype");..........................1
                 dtvehiclegetdat.DefaultView.ToTable().Clear();

                 dtgetvehicleunit = dtvehiclegetdat.Copy();
                 dtmenugetdata = dtvehiclegetdat.Copy();

                 return true;

1) "dtvehiclegetdat.DefaultView.ToTable " code is working correct order in first time calling but dtvehiclegetdat had data in second time but default view passing empty rows


0 Votes 0 ·

but i use like this now
rdr = cmd.ExecuteReader();

                  dtvehiclegetdat.Reset();
                                  
                  dtvehiclegetdat.Load(rdr);

                  Data table dtsql_load =dtvehiclegetdat.copy();

                      
                  dtcbosearch_vehicle = dtsql_load .DefaultView.ToTable(true, "Column1", "Column2");


cant know what happened here, but add one more datatable and data copy from "sql load dtvehiclegetdat "then use defaultview call repeatedly ,it is working...



anybobies know please explain....

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT Saravanaavigneshsana-2837 ·

@Saravanaavigneshsana-2837 , thanks for the response, after checking your code, I find that I still could not reproduce your problem. The code you provided is a bit confusing. I recommend that you could make a new code example that could reproduce your problem. Then you could upload the sample code to onedrive or github then share it here.

0 Votes 0 ·

0 Answers