Hello all!
I'm new to the C# language so please bare with me :)
I've created a web app that connects to a database and randomly selects rows that then goes into a .js file by streamwriter.
The .js file is used for displaying a script with data from the db.
Currently, it writes out what I need but at a limit of 7 due to a variable written in javascript by the streamwriter. I would like it to have no limitation so that it would write as many javascript variables as the count of the rows are.
How is this possible?
This is sort of what I have done so far:
SqlConnection con = new SqlConnection("Server=a;uid=a;database=tp");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM TPCPTbl ORDER BY NEWID()", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("TPCPTbl");
da.Fill(ds,"TPCPTbl");
StreamWriter sw = new StreamWriter(@"C:\\work/cp/main.js");
sw.Write(
"var c1 = ...."
//here is where I would like to have it count and give names such as c2, c3, etc depending on the number of rows
);
Last question:
How do i break lines in streamwriter?
I've been doing this: "var c1 = ..." +
"var c2 = ..."
);
The problem with this is that it doesn't break the line inside the .js file.
Thank you,
Guy
Loading
Guy MoyalPosted Jul 5, 2007, 3:18 AM
This is actually a web app :)
I've never done a windows service. I will look it up.
Thanks!
Jan MontanoPosted Jul 4, 2007, 9:44 PM
"By the way if I would want the script to run at my server and have the server automatically create the js file every 15 minutes lets say how would I do that? "
You could build it as a Windows service instead of a Windows Form Application. Also have a look at Thread.Sleep for your app's requirement to trigger every 15 minutes.
SamPosted Jul 4, 2007, 12:30 PM
Hi,
% is a mathematical operation just like +, -, * or /
x % y is the remainder when x is divided by y (y cannot be 0)
i.e
85 % 10 = 5 (remainder is 5)
10 % 10 = 0 (no remainder)
1 % 10 = 1 (remainder 1)
Sam
Guy MoyalPosted Jul 4, 2007, 12:22 PM
I understood everything except for the % part.
What does it actually mean, count what remains?
SamPosted Jul 4, 2007, 12:10 PM
I many do winform apps
:)
Guy MoyalPosted Jul 4, 2007, 12:07 PM
Thank you!
Thank you for the explanation as well!
By the way if I would want the script to run at my server and have the server automatically create the js file every 15 minutes lets say how would I do that?
SamPosted Jul 4, 2007, 12:06 PM
I'll explain.
Often you want to include numbers in strings.
i.e
int x = 1;
int y = 2;
sw.Write("x is " + x.ToString() + "; y is " + y.ToString()); // outputs x is 1; y is 2
This looks messy and is hard to read!
Therefore many methods allow you to use placeholder ({0}, {1}, {2} ...)
then list the numbers (or int variables) at the end beginning with the value of {0}.
i.e
int x = 1;
int y = 2;
sw.Write("x is {0}; y is {1}", x, y);
This is much clearer!
You can use as many placeholders as you want;
you can even use the same one twice in the same string.
However, you will get an error is you don't list enough numbers at the end of then method:
sw.Write("pc3[{0}]=Com{2} \r\n",i, ((i+2) %RowCount));
doesn't compile because 3 numbers are expected
(one for {0}, one for {1}(even though this placeholder isn't used), and one for {2})
and only two numbers are listed at the end of the method i and ((i+2) %RowCount)
Hope this helps. Post again if you have further issues.
Sam
[Edit]
placeholders don't just work for ints (though it's what I normal use them for)
p.s sorry about the long post
SamPosted Jul 4, 2007, 11:50 AM
"pc3[{0}]=Com{2} \r\n",i, ((i+2) %RowCount)
);
Should be replaced with
sw.Write(
"pc3[{0}]=Com{1} \r\n",i, ((i+2) %RowCount)
);
Guy MoyalPosted Jul 4, 2007, 11:17 AM
Index (zero based) must be greater than or equal to zero and less than the size of the argument list
This is the code:
sw.Write(
"var pc=new Array() \r\n "
);
for (int i = 0; i < RowCount; i++)
{
sw.Write(
"pc[{0}]=Com{0} \r\n", i
);
}
sw.Write(
"var pc2=new Array() \r\n"
);
for (int i = 0; i < RowCount; i++)
{
sw.Write(
"pc2[{0}]=Com{1} \r\n",i, ((i+1) % RowCount)
);
}
sw.Write(
"var pc3=new Array() \r\n"
);
for (int i=0; i < RowCount; i++)
{
sw.Write(
"pc3[{0}]=Com{2} \r\n",i, ((i+2) %RowCount)
);
}
and so on..
Guy MoyalPosted Jul 4, 2007, 10:56 AM
Excellent!!!!
Thank you very much.
I used rowcount instead of the number 4 since it relies on the number or rows.
I'll post back if I have any more problems, thanks :)
By the way could you please explain what the code meant?
SamPosted Jul 4, 2007, 10:22 AM
Hi,
");
This is what I currently think you want:
StreamWriter s = new StreamWriter("
int rowCount = ds.Tables[0].Rows.Count;
int i;
for(i = 0; i < rowCount; i++)
{
s.WriteLine("var cp[{0}] = comp{0}",i);
}
for(i = 0; i < rowCount; i++)
{
s.WriteLine("var cp2[{0}] = comp{1}",i,((i + 1) % 4));
}
This would give:
var cp[0] = comp0
var cp[1] = comp1
var cp[2] = comp2
var cp[3] = comp3
var cp[4] = comp4
...
var cp[0] = comp1
var cp[1] = comp2
var cp[2] = comp3
var cp[3] = comp0
var cp[4] = comp1 <- Is this what you want??
...
Sam.
Guy MoyalPosted Jul 4, 2007, 7:39 AM
What I am trying to do is this:
first array:
for (int i=0; i
streamwriter.write(
"var cp[i] = comp[i]"...
...
);
second array:
for (int i=1; i
streamwriter.write(
"var cp[i] = comp[i]"...
..
*here is where I need it to count the 0 after it's done 1- the number of rows so it will turn out like this "var cp[0] = comp1"
);
}
}
It's kind of a mess...
the final result of the js file should look something like this.
first array()
var cp[0] = comp0
var cp[1] = comp1
var cp[2] = comp2
var cp[3] = comp3
second array()
var cp2[0] = comp1
var cp2[1] = comp2
var cp2[2] = comp3
var cp2[3] = comp0
Thank's again for all the help.
Jan MontanoPosted Jul 4, 2007, 7:16 AM
http://www.c-sharpcorner.com/UploadFile/amehta/ControlStatements211092005022339AM/ControlStatements2.aspx
Could you tell me the actual scenario you're solving right now?
Guy MoyalPosted Jul 4, 2007, 6:14 AM
I'm having trouble with the count.
In addition to the count I am doing from row 0 I need to count a few things starting from row 2.
I've used the for (i=1; i
Is there a way to seperate counts?
Thanks, sorry for all the questions.
Guy MoyalPosted Jul 4, 2007, 4:10 AM
By the way, can I use the same count in a different place or would I have to create a new ;
for(int i2=1;....) ?
Thanks again,
Guy
Guy MoyalPosted Jul 4, 2007, 3:58 AM
You're right.
I've changed (int i=1; i < RowCount; i++ ) instead of i<= row and it seems to be working.
I will create an sw.write( var cp0=...) and then start the count right after.
Thank you very much for your help.
Hopefully I won't run into any more problems with it :)
Jan MontanoPosted Jul 4, 2007, 3:35 AM
Could you post your code snippet including the part where you're getting "There is no row at position 7." error?
Thanks.
Guy MoyalPosted Jul 4, 2007, 3:09 AM
Although, I get "There is no row at position 7." when when running it. Position 7 is the end of the table. How do I tell it to stop when it's done?
Jan MontanoPosted Jul 3, 2007, 10:58 PM
int rowCount = ds.Tables[0].Rows.Count // Tables[0] refers to your first and only table "TPCPTbl"
Moving forward...
for (int i=1; i<=rowCount; i++){
}
Guy MoyalPosted Jul 3, 2007, 4:26 PM
Great, thanks Sam!
Well, basically, what I wanted to do was have my code count the number of rows that were selected from the database and have it change the number of lets say each "var cp[1] var cp[2] var cp[3]"
For example, if there were 50 rows in the database then there would be:
"var cp1 = ... "
"var cp2 = ... "
var....................
...................
......
all the way till:
"var cp50 = ... "
SamPosted Jul 3, 2007, 3:43 PM
Don't understand main question, but lines breaks can be done with the combination \r\n
e.g.
StreamWriter s = new StreamWriter("
s.Write("Hello\r\nWorld");
Writes to
Hello
World
I think you could also use for the same result:
StreamWriter s = new StreamWriter("
s.WriteLine("Hello");
s.WriteLine("World");