Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
How to add values to datatable from parameter?I tried below code but it just added only ones and the same parameter.

C#
public DataTable create(string a)
{
            DataTable dt = new DataTable();
            dt.Columns.Add("Saheler",typeof(String));
            DataRow row = dt.NewRow();
            row[0] = a;
            dt.Rows.Add(row);
}
Posted
Updated 19-Mar-13 11:51am
v3
Comments
Akbar Ali Hussain 19-Mar-13 18:07pm    
Your code will create a table with one row and one column. So there will be only one CELL and the value of that cell will be content of "a". What else you are expecting?
Daqdibi 19-Mar-13 18:10pm    
Is it possible to add values from parameter?Any idea?
thanks in advance
Akbar Ali Hussain 20-Mar-13 10:05am    
Yes.. it is possible

1 solution

I think you need to consider splitting this

public DataTable create(string a)
{
            DataTable dt = new DataTable();
            dt.Columns.Add("Saheler",typeof(String));
            DataRow row = dt.NewRow();
            row[0] = a;
            dt.Rows.Add(row);
}


Into Something like

public DataTable create(string a)
{
            DataTable dt = new DataTable();
            dt.Columns.Add("Saheler",typeof(String));
            return dt;
}

public AddRowToDT(ref Datatable dt, String Parameter)
{
            DataRow row = dt.NewRow();
            row[0] = Parameter;
            dt.Rows.Add(row);
}


Given your description of what you're trying to do ...

If you want to elaborate further, and be a bit more specific, you might get a better answer
 
Share this answer
 
v2

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