October 21st, 2011, 11:56 AM
Newbie needs help with If statment
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:
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 void Main()
DateTime now = DateTime.Now;
DateTime lastDayofCurrentMonth = new DateTime(DateTime.Today.Year,
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
// 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.
using (System.IO.FileStream fs = System.IO.File.Create(newPath))
for (byte i = 0; i < 100; i++)
// Read data back from the file to prove
// that the previous code worked.
byte readBuffer = System.IO.File.ReadAllBytes(newPath);
foreach (byte b in readBuffer)
catch (System.IO.IOException e)
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();
DataRow selectDataRow = selectRunTbl.Rows;
decimal data = decimal.Parse(selectDataRow["Value"].ToString());
lblData.Text = data.ToString();
Thanks for any advice/help in advance.
October 22nd, 2011, 04:45 AM
Re: Newbie needs help with If statment
Use code tags when posting code. It is explained here :
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!