Radsearchbox not displaying selected text - asp.net

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.

Related

How to use select query with textbox value as compare wiith database DATE column

My .aspx markup:
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" PopupButtonID="imgpopup" runat="server" TargetControlID="TextBox11" Format="MM-yyyy" DefaultView="Months" />
<asp:TextBox ID="TextBox11" runat="server">
</asp:TextBox>
.aspx.cs code behind:
string text = "Textbox11.Text";
string s = "SELECT * FROM Stock WHERE MONTH(Date) = 09 AND YEAR(Date) = 2018";
SqlCommand cmd = new SqlCommand(s, conn);
SqlDataAdapter dr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dr.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
This is the query I got everywhere on net, but I don't want to specify the fixed date.... I want to allow the user to select text and according to that date select query should work
Please help me with is problem, as I'm new here.
Please forgive me if I asked the same question again which was asked previously by someone else
I suggest a parameterized query with an inclusive start date and exclusive end date for date range queries. It is best to avoid applying functions to SQL Server columns in a WHERE clause as that prevents indexes (if any) from used efficiently. For example:
var date = DateTime.Now;
if(!DateTime.TryParse(Textbox11.Text, out date))
{
//invalid date handing
return;
}
var startDate = new DateTime(date.Year, date.Month, 1);
var endDate = startDate.AddMonths(1);
string s = "SELECT * FROM Stock WHERE Date >= #StartDate AND Date < #EndDate;";
SqlCommand cmd = new SqlCommand(s, conn);
SqlDataAdapter dr = new SqlDataAdapter(cmd);
cmd.Parameters.Add("#StartDate", SqlDbType.DateTime).Value = startDate;
cmd.Parameters.Add("#EndDate", SqlDbType.DateTime).Value = endDate;
DataTable dt = new DataTable();
dr.Fill(dt);
Also, consider specifying an explict column list with only those columns needed rather than SELECT * as that will reduce unnecessary data transfer and give SQL Server more query optimization options.
Be aware that freeform date parsing is ambiguous. It would be better to restrict user input to a specific date format or use a date picker control that does that for you, allowing TryParseExact on the server side.

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.

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"]));

Add values from DB (Select Query) in a text. Asp.net

On my Homepage i made i Visual Studio i want to have some welcome text on my firstpage.
The Thing is that i want in the middle of the text present some values that i grab from my db with a select query.
Lets say for example:
Welcome to my Homepage!
Currently there are (Select Count(UserID) FROM users where Status = 'active') Users active on my page and (Select Count(UserID) FROM users where Status = 'inactive') that are inactive.
Im really new to this but somehow i Need to run the my questions on Page_load and then be able to take that value and present it in a Label or something?
Thanks for your help
Using ADO.NET:
1: Create a connection object, set it's connection string property
2: Create a command object and set its text property to
Select Count(UserID) FROM users where Status = 'active'
3: Use ExecuteScalar method of the command object to find the result you want and set the label text property equal to that result.
Procedure
create procedure select
#activity
as
Begin
Select Count(UserID) FROM users where Status = #activity
end
C# Page Load
string activity1="active";
string activity2="Inactive";
sqlconnection con=new sqlconnection("give your connection string");
sqlcommand cmd=new sqlcommand();
cmd=new sqlcommand("select",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#activity",activity1);
String result1=cmd.ExecuteNonQuery();
sqlcommand cmd1=new sqlcommand();
cmd1=new sqlcommand("select",con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("#activity",activity2);
String result2=cmd.ExecuteNonQuery();
Label1.Text=result1;
Label2.Text=result2;
.aspx page
Welcome to my HomePage
Currently there are <asp:label id="Label1" runat="server"></asp:Label> Users active on my page and <asp:label id="Label2" runat="server"></asp:Label> that are inactive.
If you find this as useful and it solved your issue mark it as answer and give upvote.
Try this
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(#"MY CONNECTION");
SqlCommand com = new SqlCommand("Select Count(UserID) FROM users where Status = 'active'", con);
SqlDataReader dr;
con.Open();
while (dr.Read())
{
Debug.WriteLine(dr[0].ToString());
}
con.Close();
}

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ProductID'

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.

Resources