How to display a single row in asp.net web forms? - asp.net

I ask myself how I can display a single row from a datatable object in a control like a gridview.
I already did it with label objects like here: (this is in load event. I already have buttons which increment the zero and decrement)
Tbname.Text = (dset.Tables("coduta").Rows(0).Item("Firma"))
TbStraße.Text = (dset.Tables("coduta").Rows(0).Item("Straße_Firma"))
TbHausnummer.Text = (dset.Tables("coduta").Rows(0).Item("Hausnummer_Firma"))
TbOrt.Text = (dset.Tables("coduta").Rows(0).Item("Ort_Firma"))
the point is I want to show the specific row in something like a gridview control. The only Idea i have is, to create a new table out of the row and that looks like a too complicated way for this. I hope guys can help
cheers steven

I am from C# background but this approach should help you.
Get first row from existing table.
Make clone of existing table.
Add that row to clone table.
Assign that table as datasource for grid
DataRow dr = dset.Tables("coduta").Rows(0);
DataTable dtNew = dset.Tables("coduta").Clone();
dtNew.Rows.Add(dr.ItemArray);
grid.DataSource = dtNew;
grid.DataBind();

try
da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand(#"SELECT * FROM coduta", connString);
da.Fill(ds, "coduta");
dt = ds.Tables["coduta"];
foreach (DataRow dr in dt.Rows)
{
//here is your row of data
}

Related

Radtrelist binding

I am trying to bind the simple employee table to Radtreelist but it shoes only columns not data.
source view-->
following is code that i used to bind data to Radtreelist control
void binddata()
{
cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
strquery = "select Emp_Id,Emp_Name,Emp_Salary,DeptID from Employee";
da = new SqlDataAdapter(strquery, cn);
dt = new DataTable();
da.Fill(dt);
RadTreeList1.DataSource = dt;
RadTreeList1.DataBind();
}
and table having field Emp_Id,Emp_Name,Emp_Salary,DeptID
so when i run the application it shoes only column of database
You're probably not setting the DataKeyNames and ParentDataKeyNames properties of the RadTreeList
Check the demo here.

Data Table loads only last record of mysqlDataReader

In my page mysqlDataReader returns 4 records.Whenever I load this mysqlDataReader to DataTable
using
DataTable dt = new DataTable();
dt.Clear();
dt.Load(dr);
DataTable shows only last record.
my Code is:
DataTable dt = new DataTable();
for (int i = 0; i < gvDemoBatches.Rows.Count; i++)
{
CheckBox cb = (CheckBox)gvDemoBatches.Rows[i].FindControl("checkSelect");
if (cb.Checked == true)
{
Panel2.Visible = true;
objBEBatch.BatchID = Convert.ToInt32(((Label)gvDemoBatches.Rows[i].Cells[0].FindControl("lblBatchId")).Text);
MySqlDataReader dr = objBLAddNewDemo.GetParticularDemoData(objBEBatch);
dt.Clear();
dt.Load(dr);
}
}
"GetParticularDemoData" stored Procedure Returns 7 rows.but datatable shows only last record.
Thanks in Advance,
Ratnam.
First please write proper/full code so one can understand !
According to your code, dr is a data row,and dr will contain single row at a time,
It seems you may have taken dr in loop,so at last it will contain last record only.
so your data table displays last record.!
your are adding rows to datatable inside loop. So the last loop record will be saved in the datatable
for (int i = 0; i < gvDemoBatches.Rows.Count; i++)
{
will execute multiple times and inside this you have
MySqlDataReader dr = objBLAddNewDemo.GetParticularDemoData(objBEBatch);
dt.Clear();
dt.Load(dr);
So the data table will contain the data only for the last row of the gridview where cb.Checked == true)
It looks like you are iterating your reading with clearing your table on each iteration. Try to use DataAdapter instead
var adapter = new SqlDataAdapter(query);
adapter.Fill(dt);

Databind formatted data from gridview to new gridview

I have a datatable in Gridview1 and have formatted it by adding several new rows in the datatable, I want to push this data to another table to also include the blank rows as part of the data. How would I go about doing this? What I mean is I want to use the data in my first gridview as a datasource.
Add your DataTable on into a Session.
Something like:
DataTable dt = new DataTable();
Session["DataStore"] = dt;
Gridview1.DataSource= dt;
GridView1.Databind();
Then you can use Session["DataStore"] as your datasource .
var dt = (DataTable)Session["DataStore"];
GridView2.DataSource = dt;
GridView2.Databind();
Hipe this Helps
Regards

how to add values at the end of datatable?

I am getting some data in unstructured form. So i want it to be in structured form. So i take datatable in this i have added two columns, i.e. "field" and "value". Now i added first row in data along with the data in these two columns. But the prob is its replacing the old data every time i come again to save the data in this datatable. My code is:
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Rows.Add(val.Substring(0, val.IndexOf(":") + 1), val.Substring(val.IndexOf(":") + 1, val.Length - val.IndexOf(":") - 1))
Using DataTable, you have to create a new row before adding it:
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(string));
var row = dt.NewRow();
dt["Field"] = "FieldName";
dt["Value"] = "Value";
dt.Rows.Add(row);
NewRow() documentation :
You must use the NewRow method to create new DataRow objects with the
same schema as the DataTable. After creating a DataRow, you can add it
to the DataRowCollection, through the DataTable object's Rows
property. When you use NewRow to create new rows, the rows must be
added to or deleted from the data table before you call Clear.
But if you are only handling a key/value pair, why don't you use a Dictionary<string, string> ?
Are u using this in a loop? only the last line should be in the loop otherwise youre instantiating the datatable each loop.

SqlDataSource.Select()? How do I use this? (ASP.net)

I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?
I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.
This puts the result query in to a DataTable.
DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;
foreach (DataRow dr in dt.Rows)
{
// Do something here IE grab the value of the first column
value = dr[0];
}
Repying to last question in comment:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
I was getting crazy trying to do this simple operation:
retrieving data from sqldatasource and put it into variables that I can manipulate.
At the end, Here the working behind code to do this for VB.NET:
Dim DV As New DataView()
Dim DataTable As New DataTable()
Dim SqlDataSource1 As New SqlDataSource()
Dim VALUE As String
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * from Table"
DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
DataTable = DV.ToTable()
For Each riga As DataRow In DataTable.Rows
VALUE = riga("table_name").ToString
Next
the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.
ENJOY

Resources