I am working with ASP.net. In that in one sql query my output is 21,22,23, which is a string.
I want to remove those commas and store them as separate integer values...I want to use an array. Plese help. How to do that ?
You can convert a string separated by certain characters by using .Split(char):
string test = "21,22,23";
string[] split = test.Split(',');
This will give you an array of strings though. If you want to use them as integers you will want to convert them as well, and depending on your situation you might want to check if it's parseable or not, but you could use LINQ and do something like this:
string test = "21,22,23";
int[] values = test.Split(',').Select(value => Convert.ToInt32(value)).ToArray();
the String.Split(char[]) function should do this for you. I think in ASP.net it goes:
string values = "21,22,23";
string[] valuesArray = values.split(",");
While a normal String.Split would work, you still won't get integer values. You can try a simple LINQ query like so:
var results = from string s in yourString.Split('s')
select int.Parse(s);
You can then obviously cast it to a list or array, depending on your needs... A sample for converting it to an array directly in the LINQ query is as follows:
int[] results = (from string s in yourString.Split('s')
select int.Parse(s)).ToArray();
Related
So I'm trying to check if some of my values match, for that I'm using an SQLite object and casting it as date and passing it as a parameter which is also a Date, I just want to know if this is the right way to do it?
"AND R1.TIMING = ?
AND R1.VARIETY = ?
AND R1.JOBACRES = ?
AND CAST (R1.PLANTINGDATE AS DATE) = ?
new object[] { item.TIMING, item.VARIETY, item.JOBACRES, item.PLANTINGDATE.Date }).ToList();
This is written in C#, since I want to execute this query in C# with SQLite, any inputs would be helpful
I am new to Asp.Net and hence this question which I know might sound silly but I am really reaching no where.
Is there a way to convert an array list that contains 'Datetime' values to an array list of strings ? I wanted to use the output as a part of a javascript function.
Thanks for the help !
I am using Asp.Net with VB.
Updated
Here is the snippet
Dim timeLapse As New ArrayList
Dim timelapsehour As New ArrayList
Dim DtTmOfInf As DateTime
DtTmOfInf = DateTime.Parse(temp)
.
.
.
For i = 0 To timeLapse.Count - 1
timelapsehour.Add(DtTmOfInf.AddHours(timeLapse(i)))
Next
Here the 'DtTmOfInf' stores the value of 'temp' which is a string in datetime format and the timelapsehour is that array list that stores the required date and time values.
You could do this in a one-liner even (with Linq):
Dim yourDateList As List(Of DateTime) = ...
Dim yourStringList = yourDateList.Select(Function(d) d.ToString()).ToList()
I've made some assumptions about your data types; if you could add to your question the code you use to build your list, then I can refine as necessary.
I have created an array from a CSV of date values and now need to be able to sort them so that I can then get the latest date from the array.
I have tried:
Array.Sort()
but this doesn't sort correctly, I suppose because the array values are strings, any body got any ideas??
Thanks for any help.
CODE USED TO CREATE ARRAY
'array string exampel: "19/07/2012,23/07/2012,23/07/2012,19/07/2012,25/07/2012"
Dim ArrDates As Array = ArrDates .Split(",")
SOLUTION
Dim ArrAgentsReportCheck As Array = AgentsReportCheck.Split(",")
Dim ArrDates As New List(Of Date)
For i As Integer = 0 To ArrAgentsReportCheck.Length - 1
ArrDates.Add(ArrAgentsReportCheck(i))
Next
ArrDates.Sort()
Dim LatestDate As Date = ArrDates.Max()
ArrDates = ArrDates.OrderBy(Function(d) DateTime.ParseExact(d, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)).ToArray()
Alternately, you can use OrderByDescending() depending upon your needs.
As astander said, It is very complicated to sort a array having datetime values. Instead just convert the array to List or ArrayList and make your life easy.
For ArrayList you can use the following syntax:
List<DateTime> dates = ... // init and fill
dates.Sort();
dates.Reverse();
One way would be to convert strings to DateTime using DateTime.ParseExact
Another way just to write your own IComparer and pass to Array.Sort
Problem: I have a form with text values, and a function that must return a string query based on the values of the text values too.
Solution: I created a SQLCommand query with parameters, then I put the SQLCommand.CommandText to a string and I returned it (to the business logic that is going to handle the query)
Main Question: Is it sql-injection proof?
Code Example:
sQuery = "select * from xy where x like '%#txtNameParameter%'";
SqlCommand cmd = new SqlCommand(sQuery);
cmd.Parameters.Add("#txtNameParameter", SqlDbType.VarChar);
cmd.Parameters["#txtNameParameter"].Value = txtName.Text;
string query = cmd.CommandText;
return query;
Sub question if main question is ok:
Should I put into parameters also values of a radiobutton and dropdownmenu or are they injection-proof?
What you are doing here is injection proof because you are not injecting anything. In fact, your parameter isn't even used (because the only reference to it is inside a string literal so the SQL Parser won't even see where you are attempting to use the parameter because it will treat it as a string literal.)
You may want to change that line of code to:
sQuery = "select * from xy where x like '%'+#txtNameParameter+'%'";
Which would make the SQL look like this:
select * from xy where x like '%'+#txtNameParameter+'%'
Which is just string concatenation in a place where a string is expected in the SQL command anyway.
However, your description of what you are doing with this afterwards possibly blows all that out of the water. I cannot understand why you would want to send just the where clause of the query to the business layer.
Also, the substringed WHERE clause will not contain the data you are putting in the parameter. So you are getting no more benefit that just returning
return "where x like '%#txtNameParameter%'";
The parameter value is lost.
I'm having a problem with an expression in my sql statement in SQLite for Adobe AIR
basically I have this
sql = "UPDATE uniforms SET status=#status WHERE customerId IN(19,20)";
updateStmt.parameters["#status"] = args[1];
updateStmt.execute();
if I run the above code it works, updating the status when the id are 19 and 20
but if I pass the ids list as a parameter like this
sql = "UPDATE uniforms SET status=#status WHERE customerId IN(#ids)";
updateStmt.parameters["#status"] = args[1];
updateStmt.parameters["#ids"] = "19,20";
updateStmt.execute();
it gives me and error, saying could not convert text value to numeric value, which make sense because I'm passing and string but the IN expression should convert it accordingly, like it does when I pass directly the list values, why is not working the other way, thanks for any help!
Not sure but I think the problem is that your #ids parameter should be an array when using it like this. Try
updateStmt.parameters["#ids"] = new Array(19,20);