|
Wow, who though up that twisted use of a comma separated value file? Each line should normally contain the same number of columns, and the records would be put vertically in the file, not horisontally...
Yes, you need to use a loop to get each set of values from the line:
for (int i = 10; i < col.Length; i += 5) {
' here col[i], col[i+1] ... col[i+4] contains the five values.
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hell dear and thanks for the speedy response!
I guess I can see that you are using i+=5 instead of i++ because I told you that there might be 5 column headers and then the data would be repeated after every 5 values? Is that why you have increments of 5?
Secondly, in regards to the line
Guffa wrote: here col[i], col[i+1] ... col[i+4] contains the five values
well even if we use i based syntax instead of actual numbers...we are still doing it manually...in other words I'd still have to provide values (@Col,@Col2,@Col3,@Col4,@Col5)"; myself, which incase of tens of thousands of line, may not be practical! I'm sure thats not what you are suggesting. I think I'm missing a piece here sorry Can you please elaborate where am I wrong here?
Thanks
Tina
|
|
|
|
|
Tina P wrote: I guess I can see that you are using i+=5 instead of i++ because I told you that there might be 5 column headers and then the data would be repeated after every 5 values? Is that why you have increments of 5?
Exactly.
Tina P wrote: well even if we use i based syntax instead of actual numbers...we are still doing it manually...in other words I'd still have to provide values (@Col,@Col2,@Col3,@Col4,@Col5)"; myself, which incase of tens of thousands of line, may not be practical!
The names of the parameters has nothing to do with the index of the columns where you get the values. In fact you should name the parameters something more useful, like @Name,@Address,@Phone,@SecurityLevel,@Coverage. For the @Name parameter you use the value in col[i], for the @Address parameter you use the value in col[i+1], and so on.
Create the command object and the five parameter objects outside the loop. That way you only have to assign the values to the paramters and execut the command inside the loop.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks a lot Guffa, I appreciate you explaining it to me in such a great manner
I think I'm getting it. I wrote this quickly, do you think it make sense?
SqlConnection conn = new SqlConnection("blah blah");
string query = "insert into ClientTable ([" + col[5] + "],[" + col[6] + "],[" + col[7] + "],[" + col[8] + "],[" + col[9] + "]) values (@Name,@Address,@Phone,@SecurityLevel,@Coverage)";
SqlCommand cmd = new SqlCommand(query, conn);
SqlParameter param = new SqlParameter();
param.ParameterName = "@Name";
param.ParameterName = "@Address";
param.ParameterName = "@Phone";
param.ParameterName = "@SecurityLevel";
param.ParameterName = "@Coverage";
for (int i = 10; i < col.Length; i += 5)
{
param.Value = col[i];
param.Value = col[i+1];
param.Value = col[i+2];
param.Value = col[i+3];
param.Value = col[i+4];
cmd.Parameters.Add(param);
cmd.ExecuteReader();
}
|
|
|
|
|
Yes, but you need to create five SqlParameter objects, and add them to the command outside the loop. Specify the data type and maximum size for each parameter.
You can call the Prepare method on the command after you have added the parameters, that can reduce the number of round trips to the database.
Use the ExecuteNonQuery method to execute the command when you don't want any result back.
SqlConnection conn = new SqlConnection("blah blah");
string query = "insert into ClientTable ([" + col[5] + "],[" + col[6] + "],[" + col[7] + "],[" + col[8] + "],[" + col[9] + "]) values (@Name,@Address,@Phone,@SecurityLevel,@Coverage)";
SqlCommand cmd = new SqlCommand(query, conn);
SqlParameter parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 100);
SqlParameter parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 100);
SqlParameter parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 50);
SqlParameter parameterSecurity = new SqlParameter("@SecurityLevel", SqlDbType.VarChar, 50);
SqlParameter parameterCoverage = new SqlParameter("@Coverage", SqlDbType.VarChar, 50);
cmd.Parameters.Add(parameterName);
cmd.Parameters.Add(parameterAddress);
cmd.Parameters.Add(parameterPhone);
cmd.Parameters.Add(parameterSecurity);
cmd.Parameters.Add(parameterCoverage);
cmd.Prepare();
for (int i = 10; i < col.Length; i += 5) {
parameterName.Value = col[i];
parameterAddress.Value = col[i+1];
parameterPhone.Value = col[i+2];
parameterSecurity.Value = col[i+3];
parameterCoverage.Value = col[i+4];
cmd.ExecuteNonQuery();
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks a lot Guffa, I did it slightly differently but same idea.
cmd.Parameters.Add("@DATE", SqlDbType.DateTime);
for (int i = 0; i < col.Length; i += 11)
{
cmd.Parameters["@DOW"].Value = col[i+1];
etc.
cmd.ExecuteNonQuery();
}
A slighly tangential question...I notice that the column name in the table in SQL server has spaces in them
lile Fuel Location, Pin Code etc. Now it looks like in C# (or even sql server if I try to do a query without
using [] square brackets around the name, it doesnt recognize it...the question is having said that do i need
these square brackets around the Values in the query and also the place where i"m setting the value of the parameter
Is this valid?
"insert into [dbo].[MyTable] ( Name,[PIN CODE],[SERVICE CENTER]) values (@Name,@[PIN CODE],@[SERVICE CENTER])
and are the following lines valid?
cmd.Parameters.Add("@[SERVICE CENTER]", SqlDbType.NVarChar, 50);
cmd.Parameters["@[Description]"].Value = col[i+2];
p.s. my concern is the use of these brackets when the actual field in a table has space in it?
Am i correct here?
Thank in advance, you have been great!
Tina
|
|
|
|
|
Tina P wrote: the question is having said that do i need
these square brackets around the Values in the query and also the place where i"m setting the value of the parameter
You can't use square brackets around parameter names, so the parameter names can not contain spaces.
Even field names without spaces in them may need square brackets around them if they are reserved keywords, like Name .
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks again Guffa, yeah i renamed them to simple one word with no spaces.
Now I'm running into a bigger issue though...i feel pretty embarassed but it is what it is, I just don't have enough experience in development.
So right now for development purposes I have modified that file i'm reading the data from so that it has same number of columns with the headers in the third line.
Evantually the file will look something like this:
Blah Blah, More blah, etc
Blah Blah, More blah, etc
Blah Blah, More blah, etc
These lines above needs to be ignored. I had to change the looping structure a little bit because it was throwing an array index out of bound exception.
Here is what i have now:
for (int i = 0; i < 11; i ++)
{
while ((line = sr.ReadLine()) != null)
{
string[] col = line.Split(',');
cmd.Parameters["@Name"].Value = col[i];
cmd.Parameters["@location"].Value = col[i + 1];
cmd.Parameters["@pincode"].Value = DateTime.Now.Date;
cmd.Parameters["@Date"].Value = col[i + 3];
.
.
.
.
cmd.Parameters["@eta"].Value = col[i + 10];
cmd.ExecuteNonQuery();
}
}
As you notice in the for loop, i had to hard code the range because col array hasn't been created at this time...this may not be a good idea moving forward!
Secondly, I'm doing single increments (i=i+1) as you can see because the data is going to be in it separate column right next to each other.
Lastly, i notice the code above seems to be ignoring the very first line in the line...what do you think is deriving that in the for loop?
What is your take on this, this seems to be inserting the values in the data base ok? But I don;t think the for and while loops logic is correct? Can you please elaborate on this?
Thank you so much.
Tina
|
|
|
|
|
Oh btw, i forgot to mention...sr is an instance of the streamreader class. Thanks
Tina
|
|
|
|
|
Remove the outer loop, you have no use for it as there is only one record per line. If there were several records per line, you would have to switch the loops so that the loop reading the lines was the outer loop and the loop traversing the columns was the inner loop.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hey how is everyone doing today? This is a ASP VBS question, I wasnt sure if I should post in here or in the visual basic forum so I apologize if this is the wrong place for this question.
I am trying to create a asp page that will take the text from an input and for each letter change it into a specified image. I am creating a generator so I can make a ton of animated letter images and they will be displayed instead of regular text depending on what the user enters.
Here is what I have tried already. The problem is, it replaces the c's in the "b" <img src string so it wont display the "b" image.
I know this is because it replaces the B with the img src and then the C replaces everything in it, what I want to know how can I get it to just replace what was inputed and not what gets replaced after it is submitted you know? haha that may sound confusing if you dont know what Im saying I will try to better explain.
Thanks for the help.
<%
sGlitter = Request.Form("text")
Function Glitter(sGlitter)
sGlitter = Replace(sGlitter, "b", "<img src=http://www.icecandyeffects.com/glitter2/pink1/b.gif>")
sGlitter = Replace(sGlitter, "c", "<img src=http://www.icecandyeffects.com/glitter2/pink1/c.gif>")
Glitter = sGlitter
End Function
Response.Write Glitter(sGlitter)
%>
|
|
|
|
|
icecandy wrote: question, I wasnt sure if I should post in here or in the visual basic forum so I apologize if this is the wrong place for this question.
Neither. Outdated and dead web technologies belong in the web development forum.
Why on earth would you use ASP ? Do you hate yourself ? ASP was a nightmare.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
So does each letter have a unique image then? I would build your image string separately and not use Replace. I'm not a VB coder so here's some pseudo code:
<br />
sGlitter = Request.Form("text")<br />
imageString = ""<br />
<br />
For Each Letter ltr in sGlitter<br />
imageString = imageString & "<img src=http://www.icecandyeffects.com/glitter2/pink1/" & ltr & ".gif>"<br />
End For<br />
Sorry for the inexact code, but you get the idea???
|
|
|
|
|
I am dynamically displaying thumbnail images from a folder but under each image, I have a checkbox -this is so the user can click a checkbox for each image he/she wants to delete and then click the submit button. However, I know I can get the values of checkboxes checked (true/false) but I need the filename of the image above it so I know which image to delete. I tried assigning the filename to the id of each checkbox but I get the error:
"The ID property of a control can only be set using the ID attribute in the tag and a simple value."
My code is as follows:
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlImages" runat="server" forecolor="#000000" backcolor="#ffffff" cellpadding="3" gridlines="none" width="50%">
<itemstyle font-name="tahoma,arial,sans-serif" font-size="12" backcolor="#ffffff" />
<alternatingitemstyle font-name="tahoma,arial,sans-serif" font-size="2" backcolor="#ffff00" />
<itemtemplate>
<img src="uploaded/<%# Container.DataItem %>" border="0" />
<asp:CheckBox ID="<%# Container.DataItem %>" Text="<%# Container.DataItem %>" Name="<%# Container.DataItem %>" runat="server" />
</itemtemplate>
</asp:DataList>
<input id="btnDelete" type="button" value="Delete" OnServerClick="deletePics" runat="server" />
<asp:Button ID="btnCancel" runat="server" onclick="btn_Cancel_Click"
Text="Back to Vehicles" CausesValidation="False" />
</div>
</form>
and I am binding an ArrayList of filenames on Page_Load.
Any ideas would be much appreciated.
Thanks
Lorna
|
|
|
|
|
Yes, the id can't be that complex. If your images come from a DB, use the id of the DB item.
One way or the other, you need to come up with a list of simpler values that map to your images, and use those.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Thanks but I'm just navigating through a directory and adding each filename to an ArrayList, I then bind the list to the DataList so somehow need to get the filename. I tried assigning the filename to the Name property using Name="<%# Container.DataItem %>" but when I wiew the source I get: name="dlImages$ctl09$cb3"??
So any ideas would be appreciated.
|
|
|
|
|
Are the filenames unique ? If you use Path.GetFileName, will that be unique enough ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Try Giving Id a Fixed Value Like chk1 ,and bound name and text with arraylist
|
|
|
|
|
Hi All,
I'm having a problem with a gridview that I am dynamically creating with my code behind. I have a multiline textbox for users to input data that is then displayed in the gridview. When the gridview databinds with the table, it is not displaying the new lines. I've found a lot of articles online showing how to fix this by setting HTMLEncode = "false", however because I'm not using any bound fields I don't think that applies for this situation. I'm thinking I might be able to resolve this by catching the text being read in from the file during the databind and doing a Text.Replace(Environment.NewLine, " ") which would display them correctly in the cells without actually modifying the saved text in my saved file on the server.
GridView gv1 = new GridView();
gv1.Width = 600;
gv1.DataSource = Table1;
gv1.DataBind();
gv1.HeaderRow.Height = 50;
gv1.HeaderRow.BackColor = System.Drawing.Color.Black;
gv1.HeaderRow.ForeColor = System.Drawing.Color.White;
gv1.RowStyle.Height = 100;
Can someone give me a quick example of how I could wire this up in the code behind?
Thanks!
"You're damned if you do, and you're damned if you dont" - Bart Simpson
|
|
|
|
|
I'd imagine an onitemdatabound event handler, and you'd find the textbox in each row and replace the NewLine with a br tag.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hey Christian,
I really like your Quote "Driven to the arms of OSX by Vista". I'm about to that point myself using Vista x64, however I might go for one of the Linux flavors
Do you have an example of how I would find the textbox in each row? I'm creating the gridview dynamically because the columns will change depending on the date each week.
GridView gv1 = new GridView();
gv1.Width = 600;
gv1.DataSource = Table1;
gv1.DataBind();
gv1.HeaderRow.Height = 50;
gv1.HeaderRow.BackColor = System.Drawing.Color.Black;
gv1.HeaderRow.ForeColor = System.Drawing.Color.White;
gv1.RowStyle.Height = 100;
GridView gv2 = new GridView();
gv2.Width = 780;
gv2.DataSource = Table2;
gv2.DataBind();
switch (DateTime.Today.DayOfWeek.ToString())
{
case "Monday":
gv2.Rows[0].Cells[0].BackColor = System.Drawing.Color.Aqua;
break;
case "Tuesday":
gv2.Rows[0].Cells[1].BackColor = System.Drawing.Color.Aqua;
break;
case "Wednesday":
gv2.Rows[0].Cells[2].BackColor = System.Drawing.Color.Aqua;
break;
case "Thursday":
gv2.Rows[0].Cells[3].BackColor = System.Drawing.Color.Aqua;
break;
case "Friday":
gv2.Rows[0].Cells[4].BackColor = System.Drawing.Color.Aqua;
break;
}
gv2.HeaderRow.Height = 50;
gv2.HeaderRow.BackColor = System.Drawing.Color.Black;
gv2.HeaderRow.ForeColor = System.Drawing.Color.White;
gv2.RowStyle.Height = 175;
GridView gv3 = new GridView();
gv3.Width = 600;
gv3.DataSource = Table3;
gv3.DataBind();
gv3.HeaderRow.Height = 50;
gv3.HeaderRow.BackColor = System.Drawing.Color.Black;
gv3.HeaderRow.ForeColor = System.Drawing.Color.White;
gv3.RowStyle.Height = 100;
Panel1.Controls.Add(gv1);
Panel1.Controls.Add(gv2);
Panel1.Controls.Add(gv3);
"You're damned if you do, and you're damned if you dont" - Bart Simpson
|
|
|
|
|
I am using Address control in my page which consists of
Country, state, County all are drop down lists and City, addresslines are regular text controls.
When ever i change index of Country or state or county focus is being set to the first control of the page.
Can anyone please let me know if there is anyway to set focus on the controls it self even after postback.
cheers
chandu
|
|
|
|
|
Inject javascript to set the focus on the right control after the page loads.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
|
I don't see how this is an ASP.NET question.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|