Table:Preventive
ID Site Category Facility Activity Recurrence DuePattern Start_Record
1 Langkawi NULL FIRE ALARM Sample of Breakglass Test Monthly 26 NULL
Table: PreventiveRecord
ID Date_Done Date_Due SME Status History_ID Remark
Null Null null null null null null
History_ID same with ID
Output must be in PreventiveRecord like as per below:
ID Date_Done Date_Due SME Status History_ID Remark
Null Null 26/1/2011 null null 1 null
Null Null 27/2/2011 null null 1 null
and so on...
26th -due pattern
monthly basis..
my problem :
I am tring to get data from a sqlserver database and auto generate the next due date.. but i dont noe the correct way to auto generate the due date in the Date_Due column.. please advice.. i searched google but couldn't find any example.. :-(
i've submit by tomorrow :-(... ------------------------------------------------------------------------------
private void Button2_Click(object sender, EventArgs e)
{
try
{
SqlConn.Open();
SqlComm.Connection = SqlConn;
SqlComm.CommandText = "SELECT * FROM preventiveRecord * WHERE [History_ID =''] AND [YEAR (Date_Due)] ='2011' ORDER BY Date_Due";
SqlDataReader reader = SqlComm.ExecuteReader();
while (reader.Read())
{
string id;
string date;
string due;
string sme;
string history;
string remarks;
id = reader["ID"].ToString();
date = reader["Date_Done"] .ToString();
due = reader["Date_Due"].ToString();
sme = reader["SME"].ToString();
history = reader["History_ID"].ToString();
remarks = reader["Remarks"].ToString();
}
SqlConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
//Console.WriteLine(ex.ToString());
}
}
}
}
-----------------------------------------------------------------------------
Here is d full 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 preventiveRecord * WHERE [History_ID =''] AND [YEAR (Date_Due)] ='2011' ORDER BY Date_Due";
SqlDataReader reader = SqlComm.ExecuteReader();
while (reader.Read())
{
string id;
string date;
string due;
string sme;
string history;
string remarks;
id = reader["ID"].ToString();
date = reader["Date_Done"] .ToString();
due = reader["Date_Due"].ToString();
sme = reader["SME"].ToString();
history = reader["History_ID"].ToString();
remarks = reader["Remarks"].ToString();
}
SqlConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
//Console.WriteLine(ex.ToString());
}
}
}
}
shshvaPosted Aug 4, 2011, 11:40 PM
Thanks and regards.
shshvaPosted Aug 4, 2011, 9:26 PM
If recurrance = Monthly and Date_Patteren = 26
Output in the PreventiveRecord table, column Date_Due must be auto generate the date like 26/1/2011,26/2/2011,26/3/2011,26/4/2011 .. and so on.
If recurrance = Yearly and Date_Patteren = 28
Output in the PreventiveRecord table, column Date_Due must be generate the date like.. 28/1/2011, 28/1/2012, 28/1/2013.. and soo on
Thanks and regards.
shshvaPosted Aug 4, 2011, 9:26 PM
If recurrance = Monthly and Date_Patteren = 26
Output in the PreventiveRecord table, column Date_Due must be auto generate the date like 26/1/2011,26/2/2011,26/3/2011,26/4/2011 .. and so on.
If recurrance = Yearly and Date_Patteren = 28
Output in the PreventiveRecord table, column Date_Due must be generate the date like.. 28/1/2011, 28/1/2012, 28/1/2013.. and soo on
Thanks and regards.
Abhimanyu K VatsaPosted Aug 4, 2011, 10:07 AM
well, as a reader or answerer your question is not specific. You should ask something in short words and well defined. There are many experts that can answer. but Please don't mind, still I am unable to understand your ask?
Your problem looks like very easy.
As in question, you have displayed 2 tables but in query you are using 1 table. Even your query looks completely wrong. Do one thing, list about 5 specific records with minimum columns and type the resultant records with your scenario that you are wishing to receive in data control on form. Don't write entire code, just write one table and its record and resultant records and your query.
I'm personally waiting for you reply here.
happy coding
shshvaPosted Aug 4, 2011, 9:20 AM
Could you please explain further more on this as i tried but ended up error.
Column date_due must like below:
Date_Due
26/1/2011
26/2/2011
26/3/2011
until december
Since its generate monthly.
If recurrance = monthly basis it must generate d date monthly, yearly basis it must b generate d date yearly , weekly basis means it will generate d date weekly.
Please advice.
Thank you very much.
Jiteendra SampathiraoPosted Aug 4, 2011, 7:45 AM
try this...
DateTime dtm= dateTime.AddDays(1);
if you want to store it as string string abc= dateTime.AddDays(1).Tostring();
for only date : dateTime.AddDays(1).ToShortDateString();