get single element from list in C# - asp.net

I have a list in which there are more than one items. Now I want to get first element's value of tRecordCount. I am trying, but getting an error System.FormatException: Input string was not in a correct format.
Can anyone tell me what is wrong in this code?
((HiddenField)GridViewPagingControl.FindControl("TotalRows")).Value = Convert.ToString(List.First(item => item.tRecordCount == Convert.ToInt32("tRecordCount")));
I have tried FirstOrDefault and Single too, but none is working. The return type of tRecordCount is int.
Thanks in Advance

May be what you want is this.
((HiddenField)GridViewPagingControl.FindControl("TotalRows")).Value = Convert.ToString(List.First().tRecordCount);

you have problem at statement
Convert.ToInt32("tRecordCount")
correct syntax in
Convert.ToInt32("/*valid integer value*/")
if tRecordCount is variable then this statement should be
Convert.ToInt32(tRecordCount)

What's wrong is:
Convert.ToInt32("tRecordCount")
You cannot convert string to int

Related

CAST() not changing to datetime format

I am using this SQL query to convert my _Submission_date column which of type nvarchar(max) to datetime format:
SELECT
CAST(PSCData._SUBMISSION_DATE AS DATETIME2)
FROM
PSCData
I have tried every possible way but still its giving this error:
Conversion failed when converting date and/or time from character string.
The data inside my _Submission_Date is in this form:
"2017-8-21 21:13:55.00000"
"2017-9-21 14:13:55.00000"
When I run this query it works fine:
SELECT CAST('2017-08-25' AS DATETIME);
but with this format :
SELECT CAST('2017-9-21 14:13:55.00000' AS DATETIME);
I get the same error mentioned above.
Any suggestions that how I can solve this?
Found the solution. The actual problem was the double quotes around the string which was causing the error it can be solved like this:
update PscData
SET _SUBMISSION_DATE = REPLACE(_SUBMISSION_DATE,'"', '')
select CAST(PSCData._SUBMISSION_DATE as DATETIME2)
FROM PSCData
i.e: first use the REPLACE method to remove double quotes around the string than you can use the cast method and can easily convert the "varchar" type data to "datetime" format without any problem. Plus u have to use "datetime2" in the cast method as "datetime" will not work with it.
Thanks for the help btw :)

Problems pattern matching using R regular expressions

I'm trying to extract a string using str_extract. Here is a small example of the type of string:
library(stringr)
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s='\\{\\\"type\\\"*\\}'
str_extract(gs,s)
I'd like to get a print-out of the entire string (the real string will have more characters of this type and should only return the piece I specified here). Instead I get NA. I'd be grateful for any ideas as to what I'm doing wrong. Thank you!
Does this do what you want?
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]} I DO NOT WANT THIS {\"type\":\"Not a Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s="\\{\"type\"(.*?)\\}"
result = str_match_all(gs,s)[[1]][,1]
To test, I added the string 'I DO NO WANT THIS', which should not be returned, and added a second object which type is 'not a polygon'
It returns:
"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
"{\"type\":\"Not a
Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
So only the elements requested. Hope this helps!

How to get the expression used in an Assign activity

In the line below, "ThenActivity" is an Assign activity nested inside the Then part of an If activity. Im trying to get at the expression, but this snippet isnt working.
((Assign)ThenActivity).To.Expression.ToString();
This returns "1.13: CSharpReference"
When it should read R = 44.5M, which is the expression text, how do I get at it?
The statement should read something like this
((CSharpValue)(((Assign)ThenActivity).Value.Expression)).ExpressionText
Note: You need to get the assignment, then its expression, cast that as a CSharpValue, then finally you can get the ExpressionText.

Extracting value from HTML table and applying assertion

I am trying to extract a value which is in integer form i.e 60.
I have a code that is going through each row and each column and then using getText() method retrieve the value from column.
When applying testNG assertEqual,the value is not matched as the value found is "[60 ]" instead of "[60]".
Output of trace:
The Text is 60
Exception in thread "main" java.lang.AssertionError: expected [60] but found [60 ]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:494)
at org.testng.Assert.assertEquals(Assert.java:123)
at org.testng.Assert.assertEquals(Assert.java:165)
at rough.Test.main(Test.java:83)
Can someone help me finding how I can fix the assertion?
do you in fact have a trailing space in the table?
this is an issue that is good to find in testing
perhaps you should trim the result of getText()
Maybe because your code looks something like this<td>60 </td>
You are getting that extra space. You can try something like this.
String s = node.getText().trim();
assertEqual(s, "60");

I have a problem with my SQL statement

Threads
-------
ThreadID
UsersID
Date
ThreadTitle
ThreadParagraph
ThreadClosed
Topics
-----
TopicsID
Theme
Topics
Date
Here is my statement:
StringBuilder insertCommand = new StringBuilder();
insertCommand.Append("DECLARE #TopicsID int");
insertCommand.Append("INSERT INTO Topics(Theme,Topics,Date)");
insertCommand.Append("VALUES(#topic,#subTopic,GETDATE())");
insertCommand.Append("SET #TopicsID = SCOPE_IDENTITY()");
insertCommand.Append("INSERT INTO Threads(UsersID,TopicsID,Date,ThreadTitle,ThreadParagraph,ThreadClosed)");
insertCommand.Append("VALUES(#uniqueIdentifier,#TopicsID,GETDATE(),#questionTitle,#questionParagraph,0)");
I get this:
Incorrect syntax near the keyword
'INTO'. Must declare the scalar
variable "#TopicsID". Must declare the
scalar variable "#TopicsID".
The first thing I notice is:
insertCommand.Append("VALUES('#topic,#subTopic,GETDATE()')");
might need to be:
insertCommand.Append("VALUES(#topic,#subTopic,GETDATE())");
It looks like you have some extra single quotes in there.
You need a semi-colon after DECLARE #TopicsID int
That will take care of the "incorrect syntax" and declaring the scalar variable.
And I believe that you need to remove the single quotes around your three VALUES. That is causing it to think you have supplied only one VALUE instead of three.
Try
insertCommand.AppendLine
SQL sees
DECLARE #TopicsID intINSERT INTO Topics(Theme,Topics,Date) ...
You need to separate the distinct statements
Your single quotes on the last line are wrong - its turning it all onto one string:
Change
insertCommand.Append("VALUES('#uniqueIdentifier,#TopicsID,GETDATE(),#questionTitle,#questionParagraph,0')");
to
insertCommand.Append("VALUES(#uniqueIdentifier,#TopicsID,GETDATE(),#questionTitle,#questionParagraph,0)");

Resources