locked
How To Save Notepad Data In To Database RRS feed

  • Question

  • User2033107836 posted

    Hello

    Let Us Consider Todays Date Is 20-Mar-2017

    For Example In C:\DataFolder I have 4 NotePad and the file name of notepad like this

    Mango_190317

    Orange_200317

    Apple_200317

    Mango_200317

    I want to save this data In table in asp.net page load like below coz today is 20 mar so i want to save only 20 mar data

    ItemName       Record_Instert_Date

    Orange           20-Mar-2017

    Apple             20-Mar-2017

    Mango           20-Mar-2017

    Need C# asp.net Query 

    Monday, March 20, 2017 5:35 PM

Answers

  • User-707554951 posted

    Hi asp.ambur,

    Use the following code:

                string datetoday = DateTime.Today.Day.ToString();
                string year = DateTime.Now.ToString("yyyy");
                string month = DateTime.Now.ToString("MMM");
                string day = DateTime.Now.ToString("dd");
                string time = DateTime.Now.ToString("T");
                string insertdate = day + "-" + month + "-" + year+" "+time;

    Output screenshot as below:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 22, 2017 2:11 AM

All replies

  • User303363814 posted

    What have you done so far?  What do you have a problem with?  Do you know how to create a basic web site?  Can you create a database?  Do you know how to store any data in a database?  Do you know how to read filenames?  Do you know how to do string manipulation?

    Tuesday, March 21, 2017 6:44 AM
  • User-707554951 posted

    Hi asp.ambur,

    Based on your needs, I make a working sample, you could refer to it:

    Files in DataFolder as below:

    Then I use the following code:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>

    Codebehind:

    using System.Data;
    using System.IO;
    using System.Linq;
    
    protected void Page_Load(object sender, EventArgs e)
            {
                string dirPath = @"C:\Users\v-guzou\Desktop\DataFolder";
                var fileNames4 = Directory.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories)
                    .Select(Path.GetFileName);
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] 
                             { new DataColumn("ItemName", typeof(string)),new DataColumn("Record_Instert_Date", typeof(string)) });
        
                string datetoday = DateTime.Today.Day.ToString();
                string year = DateTime.Now.ToString("yyyy");
                string month = DateTime.Now.ToString("MMM");
                string day = DateTime.Now.ToString("dd");
                string insertdate = day + "-" + month + "-" + year;
                foreach (var filename in fileNames4)
                {
                    var starindex = filename.IndexOf("_")+1;
                    string fileday = filename.Substring(starindex, 2);
                    if (fileday == datetoday)
                    {
                        dt.Rows.Add(filename.Substring(0,starindex-1), insertdate);
                    }
                    
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

    Output screenshot as below:

    Best regards

    Cathy

    Tuesday, March 21, 2017 7:33 AM
  • User2033107836 posted

    Hi Cathy Zou

    Thanks For Your Code

    I need the same code in asp.net 2.0 C# im using asp.net 2.0

    Tuesday, March 21, 2017 7:56 AM
  • User-707554951 posted

    Hi asp.ambur,

    Yes, in asp.ne 2.0, we couldn’t use linq.

    We could do that without using linq.

    Something as below:

     protected void Page_Load(object sender, EventArgs e)
            {
                string dirPath = @"C:\Users\v-guzou\Desktop\DataFolder";
                var filePath4= Directory.GetFiles(dirPath);
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2]
                             { new DataColumn("ItemName", typeof(string)),new DataColumn("Record_Instert_Date", typeof(string)) });
    
                string datetoday = DateTime.Today.Day.ToString();
                string year = DateTime.Now.ToString("yyyy");
                string month = DateTime.Now.ToString("MMM");
                string day = DateTime.Now.ToString("dd");
                string insertdate = day + "-" + month + "-" + year;
                foreach (var filepath in filePath4)
                {
                    string filename = Path.GetFileName(filepath);
                    var starindex = filename.IndexOf("_") + 1;
                    string fileday = filename.Substring(starindex, 2);
                    if (fileday == datetoday)
                    {
                        dt.Rows.Add(filename.Substring(0, starindex - 1), insertdate);
                    }
    
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

    It works well on my side.

    Best regards

    Cathy

    Tuesday, March 21, 2017 9:04 AM
  • User2033107836 posted

    In asp.net 2.0 what would be 

      var filePath4= Directory.GetFiles(dirPath);
    Tuesday, March 21, 2017 9:12 AM
  • User-707554951 posted

    Hi asp.ambur,

    You need add the reference:

    using System.IO;

    For more information, please refer to the following link:

    https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx

    Best regards

    Cathy

    Tuesday, March 21, 2017 9:15 AM
  • User2033107836 posted

    Thank You So Much Its Works Like Magic Need One Help

    Cathy Zou Thanks lot for your kind help

    i need one more column in gridview to show that file data how to do so

    Tuesday, March 21, 2017 9:20 AM
  • User2033107836 posted

    Finally i need two things from your code

    1) i need one more column in gridview to show that data in notepad contains.. how to do so

    2) Apple_210317_113043 in

    "RecordInstertDate"

    21-Mar-2017 11:30:43 AM

    Tuesday, March 21, 2017 9:26 AM
  • User2033107836 posted

    This is the complete gridview will look like

    For example each Note Pad Will have data like this of each file

    "38.9,4"
    "39.3,4.1"
    "39.8,4.3"
    "40.2,4"

    "42,6"

    I want Gridview look like this

    ItemName

    Record_Instert_Date 

    Filed1

    Filed2

    Apple

    21-Mar-2017 11:30:43 AM

    38.9        

    39.3          

    39.8          

    40.2

    42

    4

    4.1

    4.3

    4

    6

     

    Tuesday, March 21, 2017 9:39 AM
  • User-707554951 posted

    Hi asp.ambur,

    Use the following code:

      <asp:GridView ID="GridView1" runat="server" EnableModelValidation="True"  OnRowCreated="GridView1_RowCreated" >
                <Columns>              
                </Columns>
            </asp:GridView>

    CodeBehind:

     protected void Page_Load(object sender, EventArgs e)
            {
    
    
                string dirPath = @"C:\Users\v-guzou\Desktop\DataFolder";
                var filePath4 = Directory.GetFiles(dirPath);
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[4]
                             { new DataColumn("ItemName", typeof(string)),
                                 new DataColumn("Record_Instert_Date", typeof(string)),
                                 new DataColumn("Filed1", typeof(string)),
                                 new DataColumn("Filed2", typeof(string)) });
    
                string datetoday = DateTime.Today.Day.ToString();
                string year = DateTime.Now.ToString("yyyy");
                string month = DateTime.Now.ToString("MMM");
                string day = DateTime.Now.ToString("dd");
                string insertdate = day + "-" + month + "-" + year;
                foreach (var filepath in filePath4)
                {
                    string filename = Path.GetFileName(filepath);
                    var starindex = filename.IndexOf("_") + 1;
                    string fileday = filename.Substring(starindex, 2);
                    if (fileday == datetoday)
                    {
                        string readText = File.ReadAllText(filepath);
                        string[] textvalu = readText.Split(',');
                        dt.Rows.Add(filename.Substring(0, starindex - 1), insertdate, textvalu[0], textvalu[1]);
                    }
    
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
              
            }
            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                GridViewRow row = e.Row;
                // Intitialize TableCell list
                List<TableCell> columns = new List<TableCell>();
                foreach (DataControlField column in GridView1.Columns)
                {
                    //Get the first Cell /Column
                    TableCell cell = row.Cells[0];
                    // Then Remove it after
                    row.Cells.Remove(cell);
                    //And Add it to the List Collections
                    columns.Add(cell);
                }
    
                // Add cells
                row.Cells.AddRange(columns.ToArray());
    
            }

    Output screenshot as below:

    Best regards

    Cathy

    Tuesday, March 21, 2017 10:32 AM
  • User2033107836 posted

    Hi Cathy Note pad data is not showing

    Let us consider Apple - 21-Mar-2017 notepad has total below data for apple only 

    "38.9,4"
    "39.3,4.1"
    "39.8,4.3"
    "40.2,4"

    "42,6"

    It has to show like this on first row filed1 and filed2

     

    ItemName

    Record_Instert_Date 

    Filed1

    Filed2

    Apple

    21-Mar-2017 11:30:43 AM

    38.9        

    39.3          

    39.8          

    40.2

    42

    4

    4.1

    4.3

    4

    6

     

    Tuesday, March 21, 2017 11:13 AM
  • User2033107836 posted

    how to show time

    21-Mar-2017 11:30:43 AM 

    Tuesday, March 21, 2017 12:57 PM
  • User-707554951 posted

    Hi asp.ambur,

    Use the following code:

                string datetoday = DateTime.Today.Day.ToString();
                string year = DateTime.Now.ToString("yyyy");
                string month = DateTime.Now.ToString("MMM");
                string day = DateTime.Now.ToString("dd");
                string time = DateTime.Now.ToString("T");
                string insertdate = day + "-" + month + "-" + year+" "+time;

    Output screenshot as below:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 22, 2017 2:11 AM
  • User2033107836 posted

    Hi Cathy Zou I Mean

    The NotePad File Name will be Like This Apple_220317_113043

    So i want the datetime format like 22-Mar-2017 11:30:43AM

    How To Do So..?

    Wednesday, March 22, 2017 4:37 AM
  • User2033107836 posted

    Hi asp.ambur,

    Use the following code:

                string datetoday = DateTime.Today.Day.ToString();
                string year = DateTime.Now.ToString("yyyy");
                string month = DateTime.Now.ToString("MMM");
                string day = DateTime.Now.ToString("dd");
                string time = DateTime.Now.ToString("T");
                string insertdate = day + "-" + month + "-" + year+" "+time;

    Output screenshot as below:

    Best regards

    Cathy

    I Mean

    The NotePad File Name will be Like This Apple_220317_113043

    So i want the datetime format like 22-Mar-2017 11:30:43AM

    How To Do So..?

    Thursday, March 23, 2017 11:41 AM
  • User-707554951 posted

    Hi asp.ambur.

    The NotePad File Name will be Like This Apple_220317_113043

    So i want the datetime format like 22-Mar-2017 11:30:43AM

    How To Do So..?

    For your problem above, I think that my reply in the following thread could help you solve your problem.

    So, you could refer to it:

    https://forums.asp.net/p/2118248/6126254.aspx

    Best regards

    Cathy

    Wednesday, March 29, 2017 2:52 AM