I need to parse the 3 arrays into a DataTable such that I can parse them in into a ListView for assignment purposes. I have some idea that I will need to use for loop, but I cant find the relevant methods to parse them within the for loop. Thanks in advance.
OK. I don't have C# installed, but I try to help you. At first one question: How should the table look like?
Something like this: name | price | category ?
Name | Category | Price
OK. Where does cartedDataTable come from? It that a member or some global or do you wanted it to be initialized inside the function?
It is created within the class that can be used by all methods in the class
OK. Then you should do something like this inside the constructor:
cartedDataTable.Columns.Add("Name", typeof(String));
cartedDataTable.Columns.Add("Category", typeof(String));
cartedDataTable.Columns.Add("Price", typeof(Double));
If you do it inside this function and it is called twice, I don't really know what would happen to those columns.
I'll try once i got back home. Out for lunch.
Here is an example that could do the job:
!!! I don't have C# installed and therefore I could not test that - notice the links below for further reading !!!
public void prodMech()
{
string[] names = File.ReadAllLines(@"..\..\productCart.txt");
string[] categories = File.ReadAllLines(@"..\..\categort.txt");
string[] prices = File.ReadAllLines(@"..\..\transaction.txt");
// potentially for constructor
cartedDataTable.Columns.Add("Name", typeof(String));
cartedDataTable.Columns.Add("Category", typeof(String));
cartedDataTable.Columns.Add("Price", typeof(Double));
// end of potentially for constructor
for (int i = 0; i < names.length; i++) {
cartedDataTable.Rows.Add(new Object[] {names[i], categories[i], double.Parse(prices[i])});
}
}
https://msdn.microsoft.com/en-us/library/hfx3s9wd%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/z16c79x4%28v=vs.110%29.aspx
Here is another example which would not require any code to be moved out of the method:
public void prodMech()
{
string[] names = File.ReadAllLines(@"..\..\productCart.txt");
string[] categories = File.ReadAllLines(@"..\..\categort.txt");
string[] prices = File.ReadAllLines(@"..\..\transaction.txt");
DataColumnCollection tableColumns = cartedDataTable.Columns;
if (!tableColumns.Contains("Name"))
cartedDataTable.Columns.Add("Name", typeof(String));
if (!tableColumns.Contains("Category"))
cartedDataTable.Columns.Add("Category", typeof(String));
if (!tableColumns.Contains("Price"))
cartedDataTable.Columns.Add("Price", typeof(Double));
for (int i = 0; i < names.length; i++) {
cartedDataTable.Rows.Add(new Object[] {names[i], categories[i], double.Parse(prices[i])});
}
}
https://msdn.microsoft.com/en-us/library/system.data.datacolumncollection.contains.aspx
IT WORKED! Thanks for the help. Now I can work on the SQL connection part :D
Mods please don't close this thread as I may need to ask questions again. Greatly appreciate it, thank you.
Great! I didn't work with C# for over a year. :D
I'm at the very last step of my assignment. Linking the database to store necessary information.
I have encounter this error regarding the date and I have trouble converting them. Gladly appreciate for your help.
EDIT:
I have also tried to change my SQL server table to DateTime properties, but it still wont work instead it popped out a new error.
What kind of database do you have? MySQL?
Try the first answer of this thread with three parameters instead of one:
NOTE: Don't use String.Format() but the parametrized constructor of SqlCommand instead.
BTW: This is called a prepared statement. You should do some reading about that. It helps you to protect your DB against SQL injections - escaping inputs is always required.
Another thing to add ontop of @Ohban:
It is a good idea to set your primary key column (transId) as an identity column, btw. That way, you do not have to specify it in your insert statement, nor have to assign it a value-- it'll automatically increment for each new record.
If using what Ohban has recommended is and / or feels out of scope for your current situation, you could do the following for your assignment:
in the string.format line where you build your SQL query, for the tDate variable (which I assume is a Datetime object?), call it with the following method: .ToString( "yyyy-MM-dd HH:MM:ss").
This will output the date contained in your tDate object in a format that a datetime column in a SQL Server DB will accept.
What Ohban has said is enterely right though, you SHOULD use prepared statements--always. Building SQL like that is bad practice and extremely unsafe. You should read more on this.
I managed to do that and the program is able to run perfectly. Finally completed the assignment. Thanks guys!