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"]));
Related
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.
I am using radsearchbox in my application. The data source is working and showing the values in dropdown but when I select an item in the dropdown the textbox is not populating. My code is
<telerik:RadSearchBox ID="RadSearchBox1" runat="server"
Filter="StartsWith" EnableAutoComplete="true" DataTextField="Name">
</telerik:RadSearchBox>
and in code
string comtext = "select Name from tblcustomer";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand cmd = new SqlCommand(comtext, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
RadSearchBox1.DataSource = ds;
RadSearchBox1.DataBind();
Your issue might be that you are not using a SearchContext. But I think that you just need to specify the key field for the search box. Note below, the parameter DataValueField pointed at the ID for your table. (Change to your particular table id field)
<telerik:RadSearchBox ID="RadSearchBox1"
runat="server"
DataTextField="Name"
DataValueField="TableID" >
</telerik:RadSearchBox>
You would also need to change your query string to include the ID in the dataset.
string comtext = "select Name, TableID from tblcustomer";
Telerik's RadSearch demo page can give examples of how to use SearchContext if needed.
Friends i have properly bind the dropdown with dataset but it is giving this error:
my codes are:
To Bind-Data Set
DataSet ds = new ViewAction().GetAllProductInfoData();
ddlprdctname.DataSource = ds;
ddlprdctname.DataTextField = "ProductName";
ddlprdctname.DataValueField ="ProductID";
ddlprdctname.DataBind();
and GetAllProductInfoData() function is
public DataSet GetAllProductInfoData()
{
SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
cmd.CommandText = "Select ProductID ProductName,SubCategory2ID,CompanyID,Price,Quantity,Description from ProductInfo";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.Dispose();
DataConnection.CloseConnection();
return ds;
}
What is the error please hellp me to solve
You are missing a comma in your query after ProductID. As written, it is understanding ProductName to be the returned column name alias for ProductID, and not a separate column as you most likely intended.
Your query as written is equivalent to:
Select ProductID AS ProductName, SubCategory2ID, ...
You're missing a comma in your query:
cmd.CommandText = "Select ProductID, ProductName, ...
Without the comma, the query selects the ProductID column using the alias ProductName.
If you are working with gridview and got this error you can simply remove unneccessary data from of template.
<asp:CheckBox runat="server" />
as shown above remove this error.
So I'm using a SQL data source that fires a Store procedure using the selected value of a drop down list as a parameter to return one of a multitude of differing tables. Then I have a gridview that uses this SQLdatasource as it's datasource to display the returned values.
What I want is to be able to get at the column names at run time so that I can use it in a later method. However the SQLDatasource doesn't seem to have a property to get at this data and when I try to get at the columns of the Datagridview after I databind it the gridview only shows a single column (the auto-generated select column). Is there a way to get at this?
I don't know about using the SQLDatasource, but you can do this easily in the codebehind if you load your data into a DataTable. E.g.
var dt = new DataTable();
var connectionString = "your connection string";
using (var db = new SqlConnection(connectionString))
{
var command = new SqlCommand("stored_procedure_name", db);
command.CommandType = CommandType.StoredProcedure;
db.Open();
var reader = command.ExecuteReader();
dt.Load(reader);
}
var columns = dt.Columns;
Each column has a ColumnName property. You can use Linq to get the column names as an array:
var columnNameArray = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
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