Monday, March 12, 2012

how to convert to sqltype?

For example there is a DataTable dt and the first column in the firstrow is type of string, I want to assign it to SqlString as following:

SqlString s = (SqlString)dt.Rows[0][0];

The above code throws "Invalid cast" exception. My question is how tocast it to SqlString in this case? In general, how about cast othertypes? such as if the second column in the first row is type ofdecimal, then how to cast it to SqlDecimal?

SqlDecimal d ? dt.Rows[0][1]

Thanks.

I think you could do:

SqlString s = new SqlString(dt.Rows[0][0].ToString());

I'm curious why you're wanting to work with it as a SqlString?

Marcie

Labels: , , , , , , , , , , , ,

How to convert strings yymmdd (example: 060120) into regular dates (example: 01/20/06)?

I just want to do this for displaying the data using a gridview. I am working with VB.NET.

The dates need to be saved as they are (strings = yymmdd. example: 060911) so I do not want to save how the are saved in the SQL database.

But I want to display them using the gridview in the standard date format ('mm/dd/yyyy' or 'mm/dd/yy') so I will need to convert them.


I would like to know which would be a nice way to accomplish this, thanks for any suggestions.

another thing i would like to know how to do is to convert another field from the same gridview from string: (example: '2295' into formated amounts (example: $22.95) I do not care wether they are numeric or strings, because I just want it for displaying the data in the gridview. But also these values are saved without the decimal point, for example 10099, should be converted to $100.99 or 150089 should become $1,500.89

Thanks for suggestions.

remember I do not want to change or save this data into the database, because it is supposed to be saved as it is. But I do want to be able to show the user the data in the appropiate format.

The thing is that I do not know how or where to write the code to tell the gridview to convert it how I want it.

This will convert your date to a DateTime object

DateTime.ParseExact("060708","yymmdd", System.Globalization.CultureInfo.CurrentCulture ,System.Globalization.DateTimeStyles.None)

You can handle the RowDataBound event find the label you are displaying it in and set it to something like

mylabel.Text = DateTime.ParseExact("060708","yymmdd", System.Globalization.CultureInfo.CurrentCulture ,System.Globalization.DateTimeStyles.None).ToString("mm/dd/yyy")


I need help doing the rowdatabound for doing that formating

Protected Sub GridView1_RowDataBound(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.GridViewRowEventArgs)Handles GridView1.RowDataBoundTry If e.Row.RowType = DataControlRowType.DataRowThen Dim DollarAmountAs Label =CType(e.Row.FindControl("Label"), Label)
'Here I want to format the current string.text attribute. (how can i do it?)End If Catch End Try End Sub

You can try the solution above;

dim temp as string= DollarAmount.Text

DollarAmount.Text = DateTime.ParseExact(temp,"yymmdd", System.Globalization.CultureInfo.CurrentCulture ,System.Globalization.DateTimeStyles.None).ToString("mm/dd/yyy")


Thanks,Big Smile

Labels: , , , , , , , , , , ,

How to convert Class object into DataSet?

Lets say, I have a following XSD utility generated class as follows:

[XmlRoot(Namespace="http://example.com/2007/ex1")]
public class Example {
public Example () {}
public string Name;
public int Power;
}

I have to pull data from several tables(MS Access2003) and store it in a DataSet.

Now, Is it possible to use it like:

Example example = new Example();
example.Power = 42;
example.Name = "Kibo";
DataSet myDataSet = new DataSet();
myDataSet = example;

or

myDataSet.Add(example);

Somehow, to add the example object tree in myDataSet, and pull values from tables directly into myDataSet.

This will help me in doing following things:

1. I'd not need to change my existing XSD generated class structure into DataSet.

2. Pull data directly into the DataSet, using explicit queries

3. Generate the Xml file directly.

Any help/idea/pointer/etc would help Alot.

Thanks.

The sample code you have given shouldn't work cause a DataSet can't load data from an custom entity object.

Check this site for more details:

http://msdn2.microsoft.com/en-us/library/ekw4dh3f(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/ekw4dh3f(VS.80).aspx

Labels: , , , , , , , , , , , , , , ,