Radtrelist binding - asp.net

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.

Related

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

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
}

Dropdown list selected value from database

I have a table that has a TypeID column, amongst others. This TypeID column is a foreign key to another table where type names are populated.
On my aspx page, I have a drop down list whose selected field I want to set to whatever Type Name that TypeID corresponds to.
The confusing part is that I am querying table that just has the TypeID so I can't say something like below since I don't have TypeDesc in this table.
ddl.SelectedItem.Text = dtType.Rows[0]["TypeDesc"].ToString();
The way I connect to database to populate the dropdownlist
string sqlType = "SELECT TypeID, TypeDesc FROM .......";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
SqlCommand comType = new SqlCommand(sqlType, con);
comType.CommandType = CommandType.Text;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dtType = new DataTable();
da.Fill(dtType);
con.Close();
ddl.DataTextField = "TypeDesc";
ddl.DataValueField = "TypeID";
ddl.DataSource = dtType;
ddl.DataBind();
}
After above code runs, ddl is successfully populated with items. This is ddl on the aspx side:
<asp:DropDownList ID="ddl" runat="server" />
After all this is over, I query database again with a different query that selects from a table that only has TypeID (as foreign key to the above table of types).
After i execute this new query, i have a TypeID. I want the ddl to make the selected item the one whose Value matches this TypeID.
I can get all this to work with
ddl.SelectedIndex = Convert.ToInt32(dt.Rows[0]["TypeID"])-1;
but that is assuming that ddl member index is same as TypeIDs. Shouldn't there be an easier way to do this?
use this code:
ddl.SelectedValue = Convert.ToInt32(dt.Rows[0]["TypeID"]);
or
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(dt.Rows[0]["TypeDesc"]));

Cast error on GridView Sorting event

I'm intercepting the RowDatabound event and using:
DbDataRecord record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
to get access to the unbound data source columns. It works fine on page load and when I apply a filter to the dataset. However, when I attempt to initate a sort on a GridView Column, I get :
Error: Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.Common.DbDataRecord'
I think I've traced the root issue back to the bind method used in the sort:
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (eventArgs != null)
{
DataSet SortedData = new DataSet();
ds.Tables[0].DefaultView.Sort = eventArgs.SortExpression + " " + GetSortDirection(eventArgs.SortExpression);
SortedData.Tables.Add(ds.Tables[0].DefaultView.ToTable());
GridView1.DataSource = SortedData;
}
else
{
GridView1.DataSource = ds;
}
GridView1.DataBind();
}
I remember this was a major pain to figure out because only Dataset had a Sort Property
Since you assign a DataSet to GridView1.DataSource, then the type of e.Row.DataItem in the RowDatabound event will be a DataRowView instead of a System.Data.Common.DbDataRecord. You need to change this:
DbDataRecord record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
to this:
DataRowView record = (DataRowView)e.Row.DataItem;
Instead of trying to sort the Dataset, I appended the sort direction and column from the viewstate to the query before binding it to the grid using SqlDataReader. Looks like I went out of my way to make a simple thing hard to begin with...

Binding Checklistbox with SqlServerDatabase

I want to Bind a checklistbox with Database in sqlserver2008. I am working in asp.net C# on a user control Module. I wrote a code. i want to know whether the code is perfact or not and also want to know that in which event i should place this code to get proper output.
{
int Post_Id = int.Parse(ViewState["ID"].ToString());
SqlConnection cn1 = new SqlConnection();
cn1.ConnectionString=
ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("SelectTags", cn1);
DataTable ds = new DataTable();
SqlCommand cmnd1 = new SqlCommand("SelectTags", cn1);
cmnd1.Parameters.AddWithValue("#Post_Id",Post_Id);
cmnd1.CommandType = CommandType.StoredProcedure;
cn1.Open();
cmnd1.ExecuteNonQuery();
da.Fill(ds);
cn1.Close();
foreach (DataRow dr in ds.Rows)
{
String field1 = dr["Tag_Name"].ToString();
CheckBoxList2.Items.Add(field1);
CheckBoxList2.DataBind();
}
}
SQL query for sql server 2008
GO
/****** Object: StoredProcedure [dbo].[InsertPost2Tag] Script Date: 04/02/2013 09:47:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter PROCEDURE [dbo].[SelectTags]
-- Add the parameters for the stored procedure here
#Post_Id int
AS
BEGIN
SELECT mst_Tag.Tag_Name FROM mst_Tag INNER JOIN Post2Tag ON mst_Tag.tagId = Post2Tag.Tag_Id Where Post2Tag.Post_Id=#Post_Id
END
GO
Do this in page load witin
if(!ispostback){
CheckBoxList2.DataSource = ds; //This is the dataset that you fill from your stored procedure;
CheckBoxList2.DataTextField = "Tag_Name";
CheckBoxList2.DataValueField = "Tag_Name_Id";
CheckBoxList2.DataBind();
}
and take one more parameter Tag_Name_Id in your sp query..
SELECT mst_Tag.Tag_Name,Tag_Name_Id FROM mst_Tag INNER JOIN Post2Tag ON mst_Tag.tagId = Post2Tag.Tag_Id Where Post2Tag.Post_Id=#Post_Id
Remove this from your code
foreach (DataRow dr in ds.Rows)
{
String field1 = dr["Tag_Name"].ToString();
CheckBoxList2.Items.Add(field1);
CheckBoxList2.DataBind();
}
Hope this helps... If it is what you were asking for?
No need to do any thing Just choose data source for check list box and set the session variable with selected value of grid view in its selected index changed event.
It worked for me... and too easy to implement.

Why SQLCommandBuider not DELETING?

In the following code UPDATE is working but DELETE is not working. When I DELETE row it delete it and GridView1 does not show it but database still has it. customers has PrimaryKey.
using (SqlConnection connection = new SqlConnection(conString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select * from customers", connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
connection.Open();
DataTable customers = new DataTable();
adapter.Fill(customers);
// code to modify data in DataTable here
customers.Rows.RemoveAt(rowIndex);
GridView1.EditIndex = -1;
GridView1.DataSource = customers;
GridView1.DataBind();
adapter.Update(customers);
You don't want to remove the row from the DataTable but delete it.
The DataRow's DataRowState must be modified to deleted, then the DataAdapter knows that it must delete it in the datasource.
Following will delete it:
customers.Rows(rowIndex).delete();//now DataRow's DataRowState is Deleted
adapter.Update(customers); // now it's actually deleted
Futher informations: MSDN How to: Delete Rows in a DataTable
By the way, following would prevent the DataRow to be deleted, if you've changed your mind:
customers.Rows(rowIndex).RejectChanges();
The RowState will change back to Unchanged.
This works either for the DataRow or for the whole DataTable.

Resources