Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function that you return on my table
Field from the dataset how can I change my data type;
ds.Columns("dataletter").DataType = GetType(string);
C#
public DataTable letterrecive(string tblname, string Filedname, string Where, string Order)
        {
            if (Where.Trim() == "")
               Where = " 1=1 ";
            command.CommandText = "Select " + Filedname + " From " + tblname + " where " + Where + Order;
            SqlDataAdapter search1 = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            search1.SelectCommand = command;
            command.Connection = connection;
            search1.Fill(ds, "dt");
            
             return ds.Tables[0];
        }


[edit]Added pre tags [/edit]
Posted
Updated 12-Apr-11 7:45am
v4

Add the column with the right type before data binding. At data binding event convert your data to the correct type and bind to that column. Error will be occurring if you change column data type after data bound.
 
Share this answer
 
Since you don't add the columns into the data table before you do the fetch, the data type in your column in the data table will be the corresponding datatype from what's in the database.

Seems that you're trying to handle multiple different kind of queries with this method. What I actually would suggest is that instead of trying to build a 'common' fetch mechanism where you use concatenated strings, build the 'specialized' SqlCommand instances and use SqlParameters instead of literal conditions. If you like you can have a helper function to execute the commands and to fill a datatable.

If you, for some reason, need to have a string in some columns, you can also handle the conversion in the database using for example cast[^]
 
Share this answer
 
Comments
saeed1364 12-Apr-11 15:06pm    
please example better tell.
Wendelius 12-Apr-11 15:27pm    
You have already used a SqlCommand so instead of dynamically creating the command text you can define it elsewhere and pass the command object to the helper function. An example of using parameters see: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.selectcommand.aspx and for the documentation refer to http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900