Newbie needs help with If statment
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Newbie needs help with If statment

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Newbie needs help with If statment

    Greetings all,

    First of all I want you all to know that I'm very new to C# so I may not be doing this correctly.
    I am using 2005 C# 2.0 .Net version

    I am currently collecting data from a power monitor and storing that data into a SQL database.
    What I would like to do is have C# create a folder with the folder name of Power Monitor and then create a subfolder of the current month as the folder name and within that subfolder, save the database file with the day as the file name.

    I have that part working (at least I think I do). What I am needing help with is trying to make this automated so that once the program executes it will update/create these folders/files on its own.

    My thinking is that I need to include an If statement within my program. I have researched and tried various ways but I can't seem to get it working properly. Here is the code I have so far:

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

    namespace PowerMonitor
    {
    public partial class Form1 : Form
    {

    System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection
    ("data source=Chris\\SQLEXPRESS;initial catalog=Test;integrated security=SSPI;persist security info=False;workstation id=JOHN;packet size=4096");

    public Form1()
    {
    InitializeComponent();

    }
    public void Main()
    {
    DateTime now = DateTime.Now;

    DateTime lastDayofCurrentMonth = new DateTime(DateTime.Today.Year,
    DateTime.Today.Month, DateTime.DaysInMonth(DateTime.Today.Year,
    DateTime.Today.Month));


    lblDate2.Text = lastDayofCurrentMonth.ToString("MMM d yyyy");

    // Specify a "currently active folder"
    string activeDir = @"c:\PowerMonitor";

    // Creates a new subfolder under the current active folder for the current month
    string newPath = System.IO.Path.Combine(activeDir, now.ToString("MMM yyyy"));

    // Create the subfolder
    System.IO.Directory.CreateDirectory(newPath);

    // Create a new file name.

    string newFileName = now.ToString("dddd d");
    lblDate.Text = newFileName.ToString();

    // Combine the new file name with the path
    newPath = System.IO.Path.Combine(newPath, newFileName);

    // Create the file and write to it.
    // DANGER: System.IO.File.Create will overwrite the file
    // if it already exists.

    if (!System.IO.File.Exists(newPath))
    {
    using (System.IO.FileStream fs = System.IO.File.Create(newPath))
    {
    for (byte i = 0; i < 100; i++)
    {
    fs.WriteByte(i);
    }
    }

    }
    // Read data back from the file to prove
    // that the previous code worked.

    try
    {
    byte[] readBuffer = System.IO.File.ReadAllBytes(newPath);
    foreach (byte b in readBuffer)
    {
    Console.WriteLine(b);
    }
    }
    catch (System.IO.IOException e)
    {
    Console.WriteLine(e.Message);
    }
    }


    private void btnSubmit_Click(object sender, EventArgs e)
    {
    string selectRunCMD = "SELECT SUM(Raw_Current_Input) AS Value from Test";
    SqlDataAdapter selectRunAdapter = new SqlDataAdapter(selectRunCMD, sqlConnection1);
    DataTable selectRunTbl = new DataTable();
    sqlConnection1.Open();
    selectRunAdapter.Fill(selectRunTbl);
    sqlConnection1.Close();
    DataRow selectDataRow = selectRunTbl.Rows[0];
    decimal data = decimal.Parse(selectDataRow["Value"].ToString());
    lblData.Text = data.ToString();
    Main();

    }
    }
    }

    Thanks for any advice/help in advance.

    Chris

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: Newbie needs help with If statment

    Use code tags when posting code. It is explained here :

    http://www.codeguru.com/forum/showthread.php?t=401115

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center