What is the concept (# ) Coming before the select this code?
protected void btnSearch_Click(object sender, EventArgs e)
{
SqlDataSource2.SelectCommand = **#**"SELECT tblstore.storname,tblproduct.pid, tblproduct.pname, tblproduct.pprice, tblproduct.publisher, tblproduct.writer FROM tblproduct INNER JOIN tblstore ON tblproduct.storeid = tblstore.storeid WHERE tblproduct.pname LIKE # likeText;";
SqlDataSource2.SelectParameters.Add("likeText", txtName.Text);
GridView1.EmptyDataText = "not result";
}
Remove space between # and likeText from command text
SqlDataSource2.SelectCommand = #"SELECT tblstore.storname,tblproduct.pid, tblproduct.pname, tblproduct.pprice, tblproduct.publisher, tblproduct.writer FROM tblproduct INNER JOIN tblstore ON tblproduct.storeid = tblstore.storeid WHERE tblproduct.pname LIKE #likeText";
and add parameter value like this
SqlDataSource2.SelectParameters.Add("#likeText","%" + txtName.Text + "%");
Related
I have a news website , each news had an ID in my database table , I choose 4 IDs randomly to put their titles in the "see more" section .. I select the ID by generating a random row number , the problem is when I click on one of the suggested news it takes me to another news randomly ! which makes no sense to me . the weird thing is the other random ID that I'm taken to does exist but not the one that I clicked.
Latelty I noticed this : for example in the "see more" section we have title1 - title2 - title3 - title4 , so I clicked on title1 it takes me to another random news which i didn't click .. now the 4 titles in the "see more" section are different which is normal .. now when I click in title2 it will take me to the current news in title1 ! and so on next time when i click in title1 it will take me to the curent news in title2 . I can't understand the relation
I tried to store the random numbers in another variables (key1,key2..) before putting one of them in the session but It's still the same problem.
I tried also not refreshing the page (I mean depending just the post back).
I tried clearing the season also tried reomving them all before storing the news Id. There is no error messages.
int myrandom1;
int myrandom2;
int myrandom3;
int myrandom4;
string key1;
string key2;
string key3;
string key4;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
string key=" ";
key = Session["key"].ToString();
SqlConnection con = new SqlConnection("Data Source=.; initial catalog=celeblogy; integrated security=true");
SqlCommand cmd = new SqlCommand("select * from news where id like '" + key + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "new");
string Titre = ds.Tables["new"].Rows[0][1].ToString();
Titre = Titre.Replace("*", "'");
titre.InnerText = Titre;
string blog = Decode(ds.Tables["new"].Rows[0][2].ToString());
string[] Blogs;
blog = blog.Replace("*", "'");
Blogs = blog.Split('#');
Literal1.Text = Blogs[0];
Image1.Attributes["src"] = "pictures/" + key + "1.jpg";
if (Blogs.Length >= 2)
{
Literal2.Text = Blogs[1];
Image2.Attributes["src"] = "pictures/" + key + "2.jpg";
}
cmd = new SqlCommand("select * from news", con);
da = new SqlDataAdapter(cmd);
da.Fill(ds, "news");
Random rnd = new Random();
myrandom1 =rnd.Next(ds.Tables["news"].Rows.Count-1);
Image3.ImageUrl = "pictures/" + ds.Tables["news"].Rows[myrandom1][0].ToString()+"1.jpg";
Label1.Text =ds.Tables["news"].Rows[myrandom1][1].ToString().Replace("*","'");
key1 = ds.Tables["news"].Rows[myrandom1][0].ToString();
myrandom2 = rnd.Next(ds.Tables["news"].Rows.Count - 1);
Image4.ImageUrl = "pictures/" + ds.Tables["news"].Rows[myrandom2][0].ToString() + "1.jpg";
Label2.Text = ds.Tables["news"].Rows[myrandom2][1].ToString().Replace("*", "'");
key2 = ds.Tables["news"].Rows[myrandom2][0].ToString();
myrandom3 = rnd.Next(ds.Tables["news"].Rows.Count - 1);
Image5.ImageUrl = "pictures/" + ds.Tables["news"].Rows[myrandom3][0].ToString() + "1.jpg";
Label3.Text = ds.Tables["news"].Rows[myrandom3][1].ToString().Replace("*", "'");
key3 = ds.Tables["news"].Rows[myrandom3][0].ToString();
myrandom4 = rnd.Next(ds.Tables["news"].Rows.Count - 1);
Image6.ImageUrl = "pictures/" + ds.Tables["news"].Rows[myrandom4][0].ToString() + "1.jpg";
Label4.Text = ds.Tables["news"].Rows[myrandom4][1].ToString().Replace("*", "'");
key4 = ds.Tables["news"].Rows[myrandom4][0].ToString();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session["key"] =key1;
Response.Redirect(Request.RawUrl);
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Session["key"] = key2;
Response.Redirect(Request.RawUrl);
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
Session["key"] = key3;
Response.Redirect(Request.RawUrl);
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
Session["key"] = key4;
Response.Redirect(Request.RawUrl);
}
picture of the see more section
I want to filter on multiple parameters in crystal reports through combo box but the problem is one filter is active at a time.
Here is the code of index change of both combo boxes:
protected void drpUserName_SelectedIndexChanged(object sender, EventArgs e)
{
username = drpUserName.SelectedValue;
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("LeaveReport.rpt"));
rd.SetParameterValue("username", username);
rd.SetParameterValue("status", status);
rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");
CrystalReportViewer1.ReportSource = rd;
}
protected void drpStatus_SelectedIndexChanged(object sender, EventArgs e)
{
status = drpStatus.SelectedValue;
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("LeaveReport.rpt"));
rd.SetParameterValue("status", status);
rd.SetParameterValue("username", username);
rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");
CrystalReportViewer1.ReportSource = rd;
}
And here is record selection formula:
If {?username} = "-1" Then // -1 for all values
{tblEmployee.Employeer_UserName} <> {?username} // return all records
Else
{tblEmployee.Employeer_UserName} = {?username} // return selected records
and IF {?status} = "-1" Then
{tblLeave.leave_status} <> {?status}
Else
{tblLeave.leave_status} = {?status}
One more thing in above formula: if I check {?username} first and then check {?status} in this case {?username} filtering is working, but if I check {?status} first then {?status}, filtering works fine.
you can do this by this way...
protected void drpUserName_SelectedIndexChanged(object sender, EventArgs e)
{
string strSelection = "1=1";
rd.SetParameterValue("username", drpUserName.SelectedValue);
rd.SetParameterValue("status", drpStatus.SelectedValue);
if (drpUserName.SelectedValue != "-1")
{
strSelection = strSelection + "And {tblEmployee.Employeer_UserName}=" + "\""+ drpUserName.SelectedValue+"\"";
}
if ( drpStatus.SelectedValue != "-1")
{
strSelection = strSelection + "And {tblLeave.leave_status}=" +"\""+ drpStatus.SelectedValue+"\"" ;
}
rd.RecordSelectionFormula = strSelection;
rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");
CrystalReportViewer1.ReportSource = rd;
}
protected void drpStatus_SelectedIndexChanged(object sender, EventArgs e)
{
string strSelection = "1=1";
rd.SetParameterValue("username", drpUserName.SelectedValue);
rd.SetParameterValue("status", drpStatus.SelectedValue);
if (drpUserName.SelectedValue != "-1")
{
strSelection = strSelection + "And {tblEmployee.Employeer_UserName}=" + "\"" + drpUserName.SelectedValue + "\"";
}
if (drpStatus.SelectedValue != "-1")
{
strSelection = strSelection + "And {tblLeave.leave_status}=" + "\"" + drpStatus.SelectedValue + "\"";
}
rd.RecordSelectionFormula = strSelection;
rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");
CrystalReportViewer1.ReportSource = rd;
}
I have a query that executes in my C# code:
protected void btnSearch_Click(object sender, EventArgs e) {
Conn = new SqlConnection(cString);
Conn.Open();
theGender = slcGender.SelectedItem.Text;
if (slcLocation.SelectedItem.Value == "") {
locVal = slcLocation.SelectedItem.Value;
}
if (slcLocation.SelectedItem.Value != "") {
locVal = slcLocation.SelectedItem.Text;
}
if (slcSpecialty.SelectedItem.Value == "") {
speVal = slcSpecialty.SelectedItem.Value;
}
if (slcSpecialty.SelectedItem.Value != "") {
speVal = slcSpecialty.SelectedItem.Text;
}
if (slcGender.SelectedItem.Value == "") {
genVal = slcGender.SelectedItem.Value;
}
if (slcGender.SelectedItem.Value != "") {
genVal = theGender.Substring(0, 1);
}
sqlCode =
"DECLARE #strLocation varchar(200)
SET #strLocation = '" + locVal + "'
DECLARE #strSpecialty varchar(200)
SET #strSpecialty = '" + speVal + "'
DECLARE #strGender varchar(200)
SET #strGender = '" + genVal + "'
SELECT
[content_title] AS [Physician Name]
, CAST([content_html] AS XML).value('(root/Physicians/picture/img/#src)[1]','varchar(255)') AS [Image]
, dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('root/Physicians/gender'))) AS [Gender]
, CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office1/a') AS [Office1]
, CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office2/a') AS [Office2]
, CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office3/a') AS [Office3]
, CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office4/a') AS [Office4]
, dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('/root/Physicians/phone1'))) AS [Phone #]
FROM
[database].[dbo].[content]
WHERE
[folder_id] = '188'
AND
(content_html LIKE '%<gender>'+ #strGender+'</gender>%')
AND
(content_html LIKE '%'+#strSpecialty+'%')
AND
(content_html LIKE '%'+#strLocation+'%')
ORDER BY
[content_title]";
/* EXECUTE AND DISPLAY THE DATA IN THE ASP PAGE */
using(SqlCommand command = new SqlCommand(sqlCode, Conn)) {
command.CommandType = CommandType.Text;
using (SqlDataReader reader = command.ExecuteReader()) {
if (reader.HasRows) {
rptContent.DataSource = reader;
rptContent.DataBind();
}
else {
dlo.InnerHtml = "NO RESULT";
}
}
}
}
The declaration are for three separate dropdownlist in my ASP.net page. I am having an issue where If I execute the query with the above code, no result will show up, although there should be about 5 results.
The three dropdownboxes are as follow:
The All Locations, All Specialties, Any Gender has value of "" which should makes the following code search for ALL, if I understand correctly?
(content_html LIKE '%<gender>'+ #strGender+'</gender>%')
AND
(content_html LIKE '%'+#strSpecialty+'%')
AND
(content_html LIKE '%'+#strLocation+'%')
The only time it works if I fill in the gender and/or location along with the specialty, before the query gives me anything back.
How do I resolve it?
If gender is missing, it is probably missing, so there are no <gender> delimiters. Perhaps this will fix the problem:
(#strGender = '' or (content_html LIKE '%<gender>'+ #strGender+'</gender>%')) . . .
I set my report‘s data source in the backstage of the page that display the report.The code is below:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string acceptedStatus = QuoteStatusEnum.Accepted.ToString();
string rejectedStatus = QuoteStatusEnum.Rejected.ToString();
string canceledStatus = QuoteStatusEnum.Canceled.ToString();
IList<QuoteRequest> list = ServiceFactory.QuoteRequestService.QueryItems(p => (p.QuoteRequestStatus.QuoteRequestStatusName == acceptedStatus) ||
(p.QuoteRequestStatus.QuoteRequestStatusName == rejectedStatus) ||
(p.QuoteRequestStatus.QuoteRequestStatusName == canceledStatus)).OrderBy(p => p.CreatedBy).ToList();
var data = from d in list
select new
{
ClientName = d.Client.CompanyName,
Client = d.Client.ClientID,
CompanyName = d.Client.CompanyName,
QuoteNumber = d.QuoteNumber,
PropertyAddress = d.Property.PropertyAddressLine1 + (string.IsNullOrEmpty(d.Property.PropertyAddressLine2) ? "" : " ," + d.Property.PropertyAddressLine2),
QuotePrice = d.QuotePrice.HasValue ? d.QuotePrice.Value : 0,
ClientContact = ServiceFactory.ContactService.Read(p => p.ContactID == d.ClientContactID).FirstName + " " + ServiceFactory.ContactService.Read(p => p.ContactID == d.ClientContactID).LastName,
IsConverted = d.QuoteRequestStatus.QuoteRequestStatusName == "Accepted" ? "Yes" : "No",
ModifiedDate = d.ModifiedDate ?? DateTime.Now.AddYears(-10)
};
int pastDueCount = list.Count(p => p.ClientDueDate < DateTime.Today);
var instanceReporSouce = new InstanceReportSource();
instanceReporSouce.ReportDocument = new CompletedQuotesReport();
instanceReporSouce.ReportDocument.Reports.First().DataSource = data;
ReportViewer1.ReportSource = instanceReporSouce;
}
}
Now, I want to add some filter to the report.just like the effect that add filter in the design view by using "Fields.XX=parameter.value" expression,For there is no data source in the report now,so it can't be use 'Fields.XX......' expressions.How can i achieve my goal by programs in the backstage. BTW,I try to add parameter by code:
instanceReporSouce.ReportDocument.Reports.First().ReportParameters.Add(new ReportParameter("TotalAccepted", ReportParameterType.String, "TotalAccepted:" + list.Count(p => p.QuoteRequestStatus.QuoteRequestStatusName == acceptedStatus)));
I can get the parameters. but still can't filter data. How can i solve this problem?
Any suggestion is appreciated,
Best regards,
Tang.
private void RegisterByDept_NeedDataSource(object sender, EventArgs e)
{
string groupBy = string.Empty;
string sortBy = string.Empty;
Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender;
var payoutUID = report.Parameters["payoutUID"].Value.ToString();
// some code here...
}
Can anyone please tell me how to implement custom paging with datapager. Now I have managed to do this with 2 buttons (for next and previous pages) and LoadListview() method, but I would like to use this method in datapager. Any suggestions?
Code behind file:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BusinessConnectionString"].ToString());
SqlDataAdapter adap;
int startIndex;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadListview();
}
}
private void LoadListview()
{
string FindWhat = Request.QueryString["Find"];
string FindWhere = Request.QueryString["Where"];
string TownName = FindWhere;
string CountyName = FindWhere;
string PostcodeName = FindWhere;
//startIndex = int.Parse(ViewState["index"].ToString());
startIndex = MyDataPager.StartRowIndex * MyDataPager.PageSize;
int endIndex = startIndex + MyDataPager.PageSize;
adap = new SqlDataAdapter("SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY id_Business) AS MyRow, id_Business, Name, TownName FROM UbeloView WHERE (Name Like '%" + FindWhat + "%' AND TownName Like '%" + TownName + "%') UNION ALL SELECT ROW_NUMBER() OVER (ORDER BY id_Business) AS MyRow, id_Business, Name, TownName FROM UbeloView WHERE (Name Like '%" + FindWhat + "%' AND CountyName Like '%" + CountyName + "%') UNION ALL SELECT ROW_NUMBER() OVER (ORDER BY id_Business) AS MyRow, id_Business, Name, TownName FROM UbeloView WHERE (Name Like '%" + FindWhat + "%' AND PostcodeName Like '%" + PostcodeName + "%')) AS log WHERE MyRow >=" + startIndex + " AND MyRow <=" + endIndex + " ", conn);
DataSet ds = new DataSet();
adap.Fill(ds);
lstBusiness.DataSource = ds;
lstBusiness.DataBind();
}
protected void MyDataPager_PreRender(object sender, EventArgs e)
{
LoadListview();
}
This article could help you: http://www.codeproject.com/Articles/24065/Paging-ListView-With-DataPager
In this article, the author has used a DataPager_PreRender to bind his data to a ListView:
protected void DataPagerProducts_PreRender(object sender, EventArgs e)
{
ProductList db = new ProductList();
this.ListViewProducts.DataSource = db.GellAll();
this.ListViewProducts.DataBind();
}
You could try something similar:
protected void MyDataPager_PreRender(object sender, EventArgs e)
{
LoadListview();
}
Then all you will need to do is modify your LoadListview() method to get the start index from the DataPager control itself.
private void LoadListview()
{
string FindWhat = Request.QueryString["Find"];
string FindWhere = Request.QueryString["Where"];
string TownName = FindWhere;
string CountyName = FindWhere;
string PostcodeName = FindWhere;
startIndex = MyDataPager.StartRowIndex * MyDataPager.PageSize;
adap = new SqlDataAdapter("<Query removed to save space>", conn);
DataSet ds = new DataSet();
adap.Fill(ds);
lstBusiness.DataSource = ds;
lstBusiness.DataBind();
}
As you can see, I am getting the startIndex by getting the row index and multiplying it by the page size of your DataPager. Now you can just change this part of your SQL Query from startIndex * 80 to startIndex.
If all goes to plan, you won't need your btnPrevious and btnNext click events.
I hope this gives you some insight in what you are trying to achieve.