How can I add a new column and data to a datatable that already contains data?

Just keep going with your code – you’re on the right track: //call SQL helper class to get initial data DataTable dt = sql.ExecuteDataTable(“sp_MyProc”); dt.Columns.Add(“NewColumn”, typeof(System.Int32)); foreach(DataRow row in dt.Rows) { //need to set value to NewColumn column row[“NewColumn”] = 0; // or set it to some other value } // possibly save your Dataset … Read more

Access cell value of datatable

If you need a weak reference to the cell value: object field = d.Rows[0][3] or object field = d.Rows[0].ItemArray[3] Should do it If you need a strongly typed reference (string in your case) you can use the DataRowExtensions.Field extension method: string field = d.Rows[0].Field<string>(3); (make sure System.Data is in listed in the namespaces in this … Read more

Convert datatable to JSON in C#

This code snippet from Convert Datatable to JSON String in C#, VB.NET might help you. It uses System.Web.Script.Serialization.JavaScriptSerializer to serialize the contents to JSON format: public string ConvertDataTabletoString() { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(“Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true”)) { using (SqlCommand cmd = new SqlCommand(“select title=City,lat=latitude,lng=longitude,description from LocationDetails”, con)) { … Read more

How can I pass selected row to commandLink inside dataTable or ui:repeat?

As to the cause, the <f:attribute> is specific to the component itself (populated during view build time), not to the iterated row (populated during view render time). There are several ways to achieve the requirement. If your servletcontainer supports a minimum of Servlet 3.0 / EL 2.2, then just pass it as an argument of … Read more

Check if value exists in dataTable?

You can use LINQ-to-DataSet with Enumerable.Any: String author = “John Grisham”; bool contains = tbl.AsEnumerable().Any(row => author == row.Field<String>(“Author”)); Another approach is to use DataTable.Select: DataRow[] foundAuthors = tbl.Select(“Author=”” + searchAuthor + “””); if(foundAuthors.Length != 0) { // do something… } Q: what if we do not know the columns Headers and we want to … Read more

How to change DataTable columns order

Try to use the DataColumn.SetOrdinal method. For example: dataTable.Columns[“Qty”].SetOrdinal(0); dataTable.Columns[“Unit”].SetOrdinal(1); UPDATE: This answer received much more attention than I expected. To avoid confusion and make it easier to use I decided to create an extension method for column ordering in DataTable: Extension method: public static class DataTableExtensions { public static void SetColumnsOrder(this DataTable table, params … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)