Good Day Everyone..

since i have two tables..how to read from one table1 to table2..
example..Table1 with thousand over records various types of recurrance and due pattern

table1
ID   Reccurance     Duepattern
1    Monthly        10
2    Yearly         25
3    Monthly        15
4    Weekly

output must b like this in table2

History_ID     Date_Due
1              10/1/2011
1              10/2/2011
1              10/3/2011
1              10/4/2011
1              10/5/2011
1              10/6/2011
1              10/7/2011
1              10/8/2011
1              10/9/2011
1              10/10/2011
1              10/11/2011
1              10/12/2011

why dis output like this :reason History ID = ID and since reccurance yearly its read yearly and 25= duepatten

2              25/1/2011
2              25/1/2012
2              25/1/2013
2              25/1/2014
2              25/1/2015
2              25/1/2016
2              25/1/2017
2              25/1/2018
2              25/1/2019  
2              25/1/2020

why dis output like this :reason History ID = ID and since reccurance Monthly its read Monthly and 15= duepatten

3              15/1/2011
3              15/2/2011
3              15/3/2011
3              15/4/2011
3              15/5/2011
3              15/6/2011
3              15/7/2011
3              15/8/2011
3              15/9/2011
3              15/10/2011
3              15/11/2011
3              15/12/2011

my coding's output not as i expected.. Please guide me

my coding..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace PreventiveUpdater
{


    public partial class Form1 : Form
    {

        SqlConnection SqlConn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=system;Data Source=LAPTOP-SUJITA\\SQLEXPRESS");
        SqlCommand SqlComm = new SqlCommand();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConn.Open();
                SqlComm.Connection = SqlConn;
                SqlComm.CommandText = "SELECT * FROM preventive";

                SqlDataReader reader = SqlComm.ExecuteReader();

                while (reader.Read())
                {
                    string id;
                    id = reader["ID"].ToString();
                }

                //dataGridView1.DataSource = reader;
                //dataGridView1

                //SqlDataAdapter dataAdapter = new SqlDataAdapter(SqlComm);

                //SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

                //BindingSource dbBindSource = new BindingSource();



                //// Populate a new data table and bind it to the BindingSource.

                //DataTable table = new DataTable();

                //table.Locale = System.Globalization.CultureInfo.InvariantCulture;

                //dataAdapter.Fill(table);

                //dbBindSource.DataSource = table;



                //// Resize the DataGridView columns to fit the newly loaded content.

                //dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

                //// you can make it grid readonly.

                //dataGridView1.ReadOnly = true;

                //// finally bind the data to the grid

                //dataGridView1.DataSource = dbBindSource;


                SqlConn.Close();



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //Console.WriteLine(ex.ToString());
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            try
            {

                SqlConn.Open();
                SqlComm.Connection = SqlConn;
                SqlComm.CommandText = "select * From preventive Where Recurrence ='Monthly' AND DuePattern ='26'";




                SqlDataReader reader = SqlComm.ExecuteReader();

                while (reader.Read())
                {
                    //your outputs to create dataTable:
                    int ID = 1;
                    string Recurrence = "Monthly";
                    string DuePatten = "26";
                    string Date_Done ;
                    string SME;
                    string remarks;
                    string status;
                    
                  



                    //if Duepattern is a string, will change it to integer:
                    int _DuePattern = int.Parse(DuePatten);

                    DataTable table = new DataTable("preventiveRecord");
                    table.Columns.Add("ID", typeof(int));
                    table.Columns.Add("Date_Done", typeof(int));
                    table.Columns.Add("Date_Due", typeof(DateTime));
                    table.Columns.Add("SME", typeof(int));
                    table.Columns.Add("Status", typeof(int));
                    table.Columns.Add("History_ID", typeof(int));
                    table.Columns.Add("Remark", typeof(int));


                    DataRow row;
                    if (Recurrence == "Monthly")
                    {
                        for (int i = 1; i <= 12; i++)
                        {
                            DateTime date = new DateTime(DateTime.Now.Year, i, _DuePattern);
                            row = table.NewRow();
                            row["History_ID"] = ID;
                            row["Date_Due"] = date;
                            table.Rows.Add(row);

                           
                        }




                    }

                    BindingSource source = new BindingSource();
                    source.DataSource = table;

                    dataGridView1.DataSource = source;



                }

                SqlConn.Close();



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //Console.WriteLine(ex.ToString());
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        


        
    }
}

Recommended Answers

All 6 Replies

Member Avatar for stbuchok

my coding's output not as i expected.. Please guide me

You have not explained what the expected output is. Please do so.

table1
ID Reccurance Duepattern
1 Monthly 10
2 Yearly 25
3 Monthly 15

History_ID = ID
My code's output should be like as per below:

History_ID Date_Due
1 10/1/2011
1 10/2/2011
1 10/3/2011
1 10/4/2011
1 10/5/2011
1 10/6/2011
1 10/7/2011
1 10/8/2011
1 10/9/2011
1 10/10/2011
1 10/11/2011
1 10/12/2011


2 25/1/2011
2 25/1/2012
2 25/1/2013
2 25/1/2014
2 25/1/2015
2 25/1/2016
2 25/1/2017
2 25/1/2018
2 25/1/2019
2 25/1/2020

3 15/1/2011
3 15/2/2011
3 15/3/2011
3 15/4/2011
3 15/5/2011
3 15/6/2011
3 15/7/2011
3 15/8/2011
3 15/9/2011
3 15/10/2011
3 15/11/2011
3 15/12/2011

Please guide me..realy appreaciated...

thanks and regards

Output should be stored in Database.. please help me..

Output should be stored in Database table 2.. please help me..

Member Avatar for stbuchok

But that is the same output from what you said is wrong? Can you please try to be more clear about what issue you are having and what the output you currently have is?

I am guessing now.:confused:
Is the problem that the month is always 1 for the source data "2 Yearly 25"?:idea:
If this is the case then perhaps you should add a DueMonth field to the original data table or allow DuePattern to have two fields e.g. "25/3".
Then use the Day value for Month and Yearly and only use the Month value for the Yearly pattern.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.