Find a vaule from columns of a dataset asp.net - asp.net

I Want to find the value from dataset column Id.
here is the dataset
Id Value
1 football
2 Tennis
3 Cricket
If any one is absent in Column then i want to append that particular value in the dataset

I guess that is a DataTable inside a DataSet. First you need to query if the id is in the DataTable:
var dataTable = dataSet.Tables[0]; //For this example I'm just getting the first DataTable of the DataSet, but it could be other.
var id = 1;
var value = "football";
//Any(...) will return true if any record matches the expression. In this case, the expression is if a Id Field of the row is equals to the provided id
var contained = dataTable.AsEnumerable().Any(x =>x.Field<int>("Id") == id);
Then, if it's not there, add a new row:
if(!contained)
{
var row = dataTable.NewRow();
row["Id"] = id;
row["Value"] = value;
dataTable.Rows.Add(row);
}
Hope it helps

First you should use a loop to see if your dataset column 'id' contains the value. If the id is not existing then:
DataRow newrow = ds.Tables[0].NewRow(); //assuming ds is your dataset
newrow["id"] = "your new id value";
newrow["value"] = "your new value";
ds.Tables[0].Rows.Add(newrow);

Related

C# DataTable returns blank rows

Hello I am taking data from a one datatable that is coming from a session variable and trying to dump some of the data into new datatable however the new datatable returns empty? Empty meaning that there are rows present in the new table. ie., my gridview returns several blank rows?? Not sure what is happening to the string variables?
string date = DateTime.Now.ToShortDateString();
if (Session["MyData"] != null)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["MyData"];
DataTable newdt = new DataTable();
newdt.Columns.Add(new DataColumn("studentName"));
newdt.Columns.Add(new DataColumn("inter"));
newdt.Columns.Add(new DataColumn("CO"));
newdt.Columns.Add(new DataColumn("teacher"));
newdt.Columns.Add(new DataColumn("date"));
newdt.Columns.Add(new DataColumn("desc"));
DataRow newdr;
foreach (DataRow row in dt.Rows)
{
name = row["NM"].ToString();
term = row["term"].ToString();
Mark = row["Mark"].ToString();
period = row["period"].ToString();
CN = row["CN"].ToString();
CO = row["CO"].ToString();
PID = row["PID"].ToString();
//ADD TO NEW TABLE
newdr = newdt.NewRow();
name = newdr["studentName"].ToString();
dlInter.SelectedText = newdr["inter"].ToString();
CO = newdr["CO"].ToString();
teacher = newdr["teacher"].ToString();
date = newdr["date"].ToString();
LabelDescript.Text = newdr["desc"].ToString();
newdt.Rows.Add();
}
Repeater1.DataSource = newdt;
Repeater1.DataBind();
GridView1.DataSource = newdt;
GridView1.DataBind();
This line is your problem:
newdt.Rows.Add();
This will add an EMPTY row because the call to Add() without a DataRow or an object array is interpreted as adding a row without any value.
The solution is simple
newdt.Rows.Add(newdr);
Also the code before the Add is wrong because you are inverting the variables around the equal sign
newdr["studentName"] = name;
newdr["inter"] = dlInter.SelectedText;
newdr["CO"] = CO;
newdr["teacher"] = teacher
newdr["date"] = date;
newdr["desc"] = LabelDescript.Text;

How to select greater number and lower number using LINQ To SQL in C#

I have a TABLE in SQL Database there is a columns in
TABLE ID,Subject,Body,Status,TimeDate in the 400 row data and each i have take a Id as a P_Key and Identity Specification is Yes.
Here is Id = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 etc..
I want to select greater Id from table based on saved old id like i have saved ID 12 in im getting same id like this with Linq Query below:
public static int CheckId()
{
DataClassesDataContext con = new DataClassesDataContext(Globals.con);
var q = from v in con.TABLE
where v.Id== 12 & v.Status == Active
select v.Id;
foreach (var val in q)
{
return Convert.ToInt32(val);
}
return 0;
}
the i can return a greater id then 12. and there is also one issue. if there is greater ID is Missing from DB example Id 13 is missing then i will get Id 14 in that case. please let me know how can i get id like this i want from db using linq query.
Use Min
return con.<TABLE>
.Where(v=>v.ID > 12)
.Select(v=>v.ID)
.DefaultIfEmpty()
.Min();
I made a sample for you
List<Int32> test = new List<Int32>{1,2,3,4,5,6,7,8,9,10,11,12,14,13,15,16};
var min = test.Where(x=>x>12).Min();
Gives result 13 only, even when 14 is the first bigger
In Your case
//get a table object
Table table = new Table() //if you want whole row.
table = con.Table.Where(x=>x.id>12).MIN();
Based on the code you already have:
DataClassesDataContext con = new DataClassesDataContext(Globals.con);
var q = from v in con.TABLE
where v.Id > 12 & v.Status == Active
orderby v.Id
select v.Id;
return q.Take(1); // to return the whole row
// or
return q.Take(1).Id; // to return only the Id
This would return the first row meeting the criterias (id > 12, status = active). Add error handling code as needed.

Clone dataset > Change column type > Populate dataset

I have a dataset populated from a database:
dataset_original = new DataSet()
data_adapter.Fill(dataset_original)
and I cloned it:
dataset_cloned = dataset_original.Clone()
I cloned it because 1 of the columns in the original is of type int, and I want to change that to type string:
dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)
Now I need to populate the new dataset with the data from the old dataset. How do I do that?
I am using asp.net 1.1 coded with vb.net.
This simple loop should work (even with OPTION STRICT ON):
Dim dataset_cloned = dataset_original.Clone()
dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)
For i As Int32 = 0 To dataset_original.Tables.Count - 1
Dim tbl_original As DataTable = dataset_original.Tables(i)
Dim tbl_cloned As DataTable = dataset_cloned.Tables(i)
For Each row As DataRow In tbl_original.Rows
tbl_cloned.Rows.Add(row.ItemArray)
Next
Next
assuming you have only one table in the data set, you can do something like this.
int ColumnIndex = 0; //Column index of your data you want to copy
for (int i = 0; i < dataset_original.Tables[0].Rows.Count; i++)
{
dataset_cloned.Tables[0].Rows[i].SetField(ColumnIndex, dataset_original.Tables[0].Rows[ColumnIndex].ItemArray[0].ToString());
}
in the same for loop you can copy remaining columns data

How to select a particular data from a datatable?

I have three select statements,
select 1 from tbl1;
select 2 from tbl2;
select 3 from tbl3;
and in my front end i use a Datatable to retrieve the value from sql.
datatable dt = new datatable(); dt = obj.funcgetval();
gridview.DataSource = dt; gridview.Databind();
I want to select a particular data from the datatable like in a dataset we use ds.tables[0], how can we apply the same in a datatable?
You can use combination of Rows and Columns to read / write particular cell of datatable.
dataTable.Rows[rowsIndex].Column["ColumnName"] = "SomeValue";
string strValue = dataTable.Rows[rowsIndex].Column["ColumnName"].ToString();
To iterate through dataTable
foreach(DataRow row in dt.Rows)
{
string name = row["name"].ToString();
string description = row["description"].ToString();
}
If you want to select any row then use
DataRow dr=dt.Rows[Rowindex];
If you want to get any column value then
object obj=dt.Rows[Rowindex][columnindex];
or
object obj=dt.Rows[Rowindex]["ColumnName"];
This should work for you:
dt.Rows[rowindex]["ColumnName"].ToString();
If you want select specific cell value from Datatable,you can use..
dt.Rows[row index]["Column name"].TOString();
like if you want to select 1st row value from column say Name then use
dt.Rows[1]["Name"].TOString();
if u want select particular row itself you can use Select with filtercondition.
string FilterCond = ur condition;
DataRow[] myrow = dt.Select(FilterCond);

How to get value of cells from DataTable

i got a select query that returns one row.
here is my DAL function:
public Test.RequestsDataTable getRequestById2(int rid)
{
RequestsTableAdapter adapter2 = new RequestsTableAdapter();
Test.RequestsDataTable dt2 = adapter2.GetData(rid);
return (dt2);
}
Test.xsd is tableAdapter.
my question is how do i call it from code behind file and get each cell value and store it in textbox.
It's a method. Call it.
It returns a Test.RequestsDataTable. If your table has columns Column1 and Column2, then you can do:
var dt = getRequestById2(123);
string col1 = dt.Column1;
int col2 = dt.Column2;
If that doesn't answer your question, then you need to be more clear in your question.
i wrote it in cs file:
Test.RequestsDataTable rdt = new DalObj().getRequestById2(111);
rdt.Columns[0] -> returns DataColumn so i defined
DataColumn c1 = rdt.Columns[0];
but i get the column name and not it's value.

Resources