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 sqldatasource to other type like dataview, dataset or datareader

Hi all,

i have got many problems with sqldatasource and dataview

--------------------------------
string ConnectionString = "Server=server;Integrated
Security=True;Database=contest;Persist Security Info=True";

string QueryString = "SELECT * FROM [members]";

System.Web.UI.WebControls.SqlDataSource SqlDataSource = new System.Web.UI.WebControls.SqlDataSource();

SqlDataSource.ConnectionString = ConnectionString;
SqlDataSource.SelectCommand = QueryString;
SqlDataSource.DataSourceMode = System.Web.UI.WebControls.SqlDataSourceMode.DataSet;

DataView dv = new DataView();

dv = (DataView)SqlDataSource;
--------------------------------

CS0030: Cannot convert type 'System.Web.UI.WebControls.SqlDataSource' to 'System.Data.DataView'

--------------------------------

i got similar problems if i use DataSet and DataReader.
SqlDataSource.DataSourceMode = System.Web.UI.WebControls.SqlDataSourceMode.DataSet;
SqlDataSource.DataSourceMode = System.Web.UI.WebControls.SqlDataSourceMode.DataReader;

--------------------------------

thx a lot.To return the DataSet from the SqlDataSource you have to use the SqlDataSource Select method. The Select method will return a DataView if the DataSourceMode of the SqlDataSource is set to System.Web.UI.WebControls.SqlDataSourceMode.DataSet.

If you set the mode to System.Web.UI.WebControls.SqlDataSourceMode.DataReader. A DataReader will be returned.
thx,
but, i dont know how to use sqldatasource as dataview after set it to dataset.
u have an example ?
thx
What is your purpose with the code? Do you only want to access a data source and get a Dataset back? If so I should recommend you to use SqlDataAdaptar:


DataSet ds = new DataSet();

using(SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees", myConnectionString))
{
adapter.Fill(ds);
}

If you want to use SqlDataSource for some reason, here is an example:


string ConnectionString = "Server=localhost;Integrated Security=True;Database=Northwind;User=****;password=****;";
string QueryString = "SELECT * FROM [Employees]";
System.Web.UI.WebControls.SqlDataSource SqlDataSource = new System.Web.UI.WebControls.SqlDataSource();
SqlDataSource.ConnectionString = ConnectionString;
SqlDataSource.SelectCommand = QueryString;

DataSourceSelectArguments arg = new DataSourceSelectArguments();
System.Data.DataView dv = (System.Data.DataView)SqlDataSource.Select(arg);


okay thanks, now continuing on with system.data.dataview dv = ..., how do you retrieve a value, can you finish the code for us?
Many thanks,
Asaf

Here is the thing

DataView dv ;

dv = (DataView)DsOpenOrders.Select(DataSourceSelectArguments.Empty);

dv.Sort = SortField;

return dv;

DsOpenOrders is the SqlDataSource.

THis will help you I guess.

Thanks
Suda

Labels: , , , , , , , , ,

How to convert lower case to upper case

Hi everyone

How can I convert lower case to upper case when i type inside a textbox

Please provide me with vb.net code

Do you mean you want to convert it as it is being typed? Or once you receive it on the server-side?

If the latter, use the string.ToUpper() method in VB.NET.http://support.microsoft.com/kb/312897 If the former, you can use the javascript ToUppercase() method in conjunction with the onkeypress event for the textbox in question.http://jennifermadden.com/javascript/stringUpperLower.html


To achieve this on the Client side try this:

add this to your Code behind (VB.net):

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

TextBox1.Attributes.Add("onkeyup","toUppercase()")

EndSub

Then in your HTML markup:

<headrunat="server">
<script
>
function
toUppercase() {
document.form1.TextBox1.value = document.form1.TextBox1.value.toUpperCase();
}
</script
>
</head
>
<body
>
<formid="form1"runat
="server">
<div
>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox
>
</div
>
</form
>
</body
>

If you want to do this Server Side (VB.net):

TextBox1.Text = TextBox1.Text.ToUpper



you can use style instead..

<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.test
{
text-transform: uppercase;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div
</div>
<asp:TextBox ID="TextBox1" runat="server" CssClass="test"></asp:TextBox>
</form>
</body>


kaushalparik27:

you can use style instead..

 .test
{
text-transform: uppercase;
}

That won't convert it. It will only change the way it appears on a web page. If you look at the source of the page where your style is used, you will see that the case of the text is still in its original state.


It works, but it saves the data with lower case in the database


have you tried this:

scott@.elbandit.co.uk:

To achieve this on the Client side try this:

add this to your Code behind (VB.net):

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

TextBox1.Attributes.Add("onkeyup","toUppercase()")

EndSub

Then in your HTML markup:

<headrunat="server">
<script
>
function
toUppercase() {
document.form1.TextBox1.value = document.form1.TextBox1.value.toUpperCase();
}
</script
>
</head
>
<body
>
<formid="form1"runat
="server">
<div
>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox
>
</div
>
</form
>
</body
>

If you want to do this Server Side (VB.net):

TextBox1.Text = TextBox1.Text.ToUpper



pam.rose:

but it saves the data with lower case in the database

but at the time of saving in the database.. simply .ToString().ToUpper() can be used...

Labels: , , , , , , , , ,

How to convert empty string to Null of Datetime data type

Hi,
This is very annoying to me.
I have a datatable which has two fields of DateTime data type. Both of them are nullable. When I do the update to that datatable, in some cases, Null values are going be put through TextBox and DropDownList's selected Item. But the updated value in database is 01/01/1900 and 12:00 AM which is not the real null value, can anyone help me with this?
My update code:

Dim inputDateAsNewDate
Dim inputSessionAsNewDate

If Trim(txtDate.Text) <> ""Then
inputDate = Convert.ToDateTime(txtDate.Text)
Else
inputDate = System.Data.SqlTypes.SqlDateTime.Null -->01/01/1900
EndIf

If ddlSession.SelectedItem.Text <> ""Then
inputSession = Convert.ToDateTime(ddlSession.SelectedItem.Text)
Else
inputSession = System.Data.SqlTypes.SqlDateTime.Null -->12:00 AM
EndIf

UpdateDT(AgendaItemID, inputDate, inputSession)

Just out of interest, why aren't inputSession and input of type Date System.Data.SqlTypes.SqlDateTime?
If you use the standard CLR DateTime structure, it will also run back to its default value as that's all it can hold.
Make the variables of the appropriate type and then convert the input into SqlDateTime, not the other way around, and your problem should go away.

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