Showing posts with label column. Show all posts
Showing posts with label column. Show all posts

Saturday, March 31, 2012

String to DateTime?

If i have this in my Database in a column of VARCHAR

2006/12/28 18:35:10

can i convert it to date time in C# ? I attempted this?

int

result =DateTime.Compare((DateTime)ds2.Tables[0].Rows[0].ItemArray[1], (DateTime)ds2.Tables[0].Rows[1].ItemArray[1]);

which basically is taking two values in that format and comparing them but i get a invalid cast exception. And importing them into SQL As datetime is not possible.

do i have to convert it to an int first? or strip out the "/" and the ":" ? is it even possible?

thanks in advance,

mcm

UseDateTime.TryParse.

Yeah Man,

worked like a charm.

mcm


you can use

Convert.ToDateTime(string)


Convert.ToDateTime produced the same cast error as my other attempt. by TryParse worked.

yay

Wednesday, March 28, 2012

String with double quotes

Hello Everyone,

I have a little problem. I have to insert a string into a column of a database object.

For exmaple my string is : Dim filename as string = "C:\Temp\Hello.txt".. but my problem is I have to insert into db "path="C:\Temp\Hello.text"".

I don't know how to include double quote in my string. Please help.

to add a quote character to a quoted string you need to escape the quote

in vb this is done by doubling up the quote

for example to get this string:

"this string contains one quote character -> " <- see"

you actually need to use:

"this string contains one quote character -> "" <- see"

"path=""C:\Temp\Hello.text"""


Hellombanavige,

Thanks for your reply.. Your answer solved half of my problem. Now suppose if i have to add a value in between those string. For example

From your example "this string contains one quote character -> "" . If i have a string variable say dim x as string = "Hello" and I have to add this in between the present string and get the double quotes. How do i do this. Thanks a lot.


any doublequote that needs to be treated as a literal character needs to be doubled up.

dim x as string = "Hello"

dim y as string = "Testing literal quote -> """ & x & """ <-another literal quote..."

y actually equals: Testing literal quote -> "Hello" <-another literal quote...


Thanks for your help...

string.format gridview cell

I am trying to use the following code to format specific cells in my gridview and failing. I can successfully format a column by setting htmlencode off and string.format for the field, but I need to control the formatting at the cellular level :-). I can get font, color etc, but not string.format. I can add to the string i.e. in the code below everything works, except the <code> "

Case"Sales"

For i = 1To e.Row.Cells.Count - 1

IfNot RTrim(e.Row.Cells(i).Text.ToString) =""Then

e.Row.Cells(i).Text =String.Format("{0:c}", e.Row.Cells(i).Text)

EndIf

Next

"</code>

Also if not rtrim(e.row.cells(i).text.tostring = "" does not do anything for me, what is the correct syntax so that empty cells do not recieving this formatting?

<code>

Sub gridview2_rowdatabound(ByVal senderAsObject,ByVal eAs GridViewRowEventArgs)Handles GridView2.RowDataBound

Dim iAsInteger

If e.Row.RowType = DataControlRowType.DataRowThen

e.Row.Cells(4).Font.Bold =True

e.Row.Cells(8).Font.Bold =True

e.Row.Cells(12).Font.Bold =True

e.Row.Cells(16).Font.Bold =True

e.Row.Cells(17).Font.Bold =True

e.Row.Cells(4).BackColor = Drawing.Color.LightGray

e.Row.Cells(8).BackColor = Drawing.Color.LightGray

e.Row.Cells(12).BackColor = Drawing.Color.LightGray

e.Row.Cells(16).BackColor = Drawing.Color.LightGray

e.Row.Cells(17).BackColor = Drawing.Color.LightGray

SelectCase RTrim(e.Row.Cells(0).Text)

Case"Sales"

For i = 1To e.Row.Cells.Count - 1

IfNot RTrim(e.Row.Cells(i).Text.ToString) =""Then

e.Row.Cells(i).Text =String.Format("{0:c}", e.Row.Cells(i).Text)

EndIf

Next

Case"Gross Profit"

For i = 1To e.Row.Cells.Count - 1

IfNot e.Row.Cells(i).TextIsNothingThen

e.Row.Cells(i).Text ="$" & e.Row.Cells(i).Text

EndIf

Next

EndSelect

EndIf

EndSub

</code>

Thanks

if you change your trouble code to this:

String str =String.Format("{0:c}", e.Row.Cells(i).Text);
e.Row.Cells(i).Text =String.Format("{0:c}", e.Row.Cells(i).Text)

and debugg it,

what result to you get for str?
Does it format it?
Does your code even go into the if statement above the trouble line?

foregive my ignorance, is there a way to see the value of a variable in vs.net during a debugging session besides assigning it to a label in my web app?

I do know that the code does go into the if statement as changing the code to me e.row.cells(i).text = "$" & e.row.cells(i).text gives me the expected result in the gridview cell.

Also the app compiles without error.

Thanks in advance for your assistance.


Ok, I placed a break immediately after the str1 = string.format...

and in my vs.net 2005 it shows the number with no formatting.

"Good news is I now see how to trouble shoot my code without adding extra labels etc on my application to display variables just so that I know what is being assigned to the variable" Bad news, I still don't see why string.format is not working.


Oops. Disregard this post, plz.


Try this:

IfNot RTrim(e.Row.Cells(i).Text.ToString) =""Then

e.Row.Cells(i).Text =String.Format("{0:c}", Double.Parse(e.Row.Cells(i).Text))

EndIf


that works perfectly. Thank you