Click here to Skip to main content
15,904,926 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to retrieve the selected row to a separate text file I tried but my code just retrieves all the rows not the selected one
please how can i do the selection process???

What I have tried:

This is the controller action
public ActionResult ConvertToTextfile()

          string mySQL;
          DataTable table = new DataTable();
          DataSet myDataSet = new DataSet();
          mySQL = "Select * From MOUVEMSSE.Mouvem11 ";
          string connect = System.Configuration.ConfigurationManager.AppSettings["dsn_AS400"];
          string conn_string = connect + "UID=" + HttpContext.Session["Username"] + ";PWD=" + HttpContext.Session["Password"] + ";";

          db_tools.sg_connStr = conn_string;
          connection = db_tools.Connect();
          table = db_tools.ExecuteSelectCommand(mySQL, CommandType.Text);

          string folderLocation = @"C:\Users\zarai\source\repos\Sms_Chauffeur\Sms_Chauffeur\Extractedfiles"; // your path to save the files

          for (int i = 0; i<table.Rows.Count; i++)  // iterate each rows to get the value
             string fileName = folderLocation + "\\" + "File-" + i + ".txt";  // file name for saving each row
             string contents = "";
             foreach (DataColumn col in table.Columns)  // iteate each column to get its corresponding value
                 contents += table.Rows[i][col].ToString() + "    ";  // concatenating the contents

             System.IO.File.WriteAllText(fileName, contents);  // create a text file and copy the contents

          return View("data Extracted sucssessfuly");

And this is the view code
<pre> <table id="example" class="display" style="width:100%">
                <!-- <th>-</th>-->

            @foreach (DataRow row in Model.Tables[0].Rows)
                <tr class="active-row">
                    <!-- <td><input type="checkbox" /> </td>-->

                        <button type="submit" class="btn btn-primary" value=Edit>EDIT</button>

@using (Html.BeginForm("ConvertToTextfile", "Display", FormMethod.Get))
        <input type="submit" value="Submit" />
Updated 23-Dec-22 20:20pm
Richard Deeming 22-Dec-22 6:54am    
A DataTable doesn't have any concept of "selected rows", and there is nothing in your UI to allow the user to select the rows.

Click the green "Improve question" link and explain precisely what you mean by "selected rows", and how you know whether they are "selected" or not.
Imene ZARAI 22-Dec-22 7:02am    
I'am wondering if there is a way to check the row that I want to retrieve it I mean I want to choose from rows
I displayed the datatable and i want choose by selection

@foreach (DataRow row in Model.Tables[0].Rows)

Richard Deeming 22-Dec-22 7:25am    
If your rows have a primary key, you could use a checkbox with its value set to that primary key, with the whole list wrapped in a form. When you post the form, the primary keys of the selected rows will be sent to the server.
Imene ZARAI 22-Dec-22 8:06am    
I am not working with model I am using AS400 for database it's not like sql so i have manage my code just by controller and view that't why I use dataset
PIEBALDconsult 22-Dec-22 10:12am    
Perhaps the DataTable.DefaultView.RowFilter would be of use?

1 solution

The DataTable class isn;t directly displayable, so there is no mechanism for the user to select any rows within the DT, or for the DT itself to associate any rows with a "selection"

Selection is a user interface concept, and needs a data aware control to display the DT content and allow selection to proceed.
For example, the Winforms DataGridView control provides the SelectedRows collection containing DataGridViewRow objects, which have the DataBoundItem which is the actual DataRow that the DGV is displaying it from.
Share this answer
Imene ZARAI 22-Dec-22 8:43am    
I am not working with ASP.Net APPlication C# MVC I generate the interface in the view which is written by html, css,javascript code I make the display of the datatable just i don't know how to make the selection of row
Richard Deeming 22-Dec-22 10:17am    
Then why have you tagged your question with both "MVC" and "ASP.NET 4"?

And why is your code a controller method, returning ActionResult, if you're not using ASP.NET MVC?

Remember, all we have to work with is what you put in the question. If you show us ASP.NET MVC code, and tag your question as ASP.NET MVC, then you should expect to receive answers for ASP.NET MVC, not some random other platform.
Imene ZARAI 22-Dec-22 10:24am    
I mean i am not using model in my work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900