Where can I create this object in the asp.net lifecycle methods without receiving an out of range exception. Right now the only place that I can actually get a resource collection containing values is in onreasourcefetched method for webschedule info. But I need to do this before webscheduleinfo is created and it's views are populated with users.
protected void Page_Init(object sender, EventArgs e)
{
ResourcesCollection resources = WebScheduleInfo1.VisibleResources;
int count = resources.Count;
Resource obje = (Resource)resources.GetItem(1);
string name = obje.Name;
resources.Clear();
resources.Add(obje);
this.WebScheduleInfo1.ActiveResourceName = name;
}
You are getting a count of resources but you are not checking to make sure that count is greater than 0.
(Resource)resources.GetItem(1) will fail unless the resources collection has at least 2 items in it.
The collection is 0 based, so if you want the first item do something like this:
protected void Page_Init(object sender, EventArgs e)
{
ResourcesCollection resources = WebScheduleInfo1.VisibleResources;
int count = resources.Count;
if( count > 0 )
{
Resource obje = (Resource)resources.GetItem(0);
string name = obje.Name;
resources.Clear();
resources.Add(obje);
this.WebScheduleInfo1.ActiveResourceName = name;
}
}
Related
i want create purchase cart in list view and use stepper for change count of products and change online price but i do not know what to send product id to server.my code:
You can retrieve the model using the control's binding context. Then access the corresponding ID or other properties there:
private void OnStepperValueChanged(object sender, ValueChangedEventArgs e)
{
Stepper stepper = sender as Stepper;
var model = stepper.BindingContext;
// model.id
// model.Count
}
To get the current selected value in the stepper you can just:
void OnStepperValueChanged(object sender, ValueChangedEventArgs e)
{
double count = e.NewValue;
}
I am trying to build an application in asp.net which displays the real time data .Assume this application as application which you typicaly see in DMV where it dsiplays which person has to go for which counter and what is the status.
what i currently have ?
1) data displaying from database and getting the real time data
2) I am refreshing the page every 5 seconds
what i am trying to acheive?
1) Lest say i have 20 rows/persons . I want to display only 10 rows first and on page refresh next 10 rows and again on next page refresh go back to 1st 10 rows.
protected void Page_Load(object sender, EventArgs e)
{
BindQueueGrid();
GetScrollInfo();
gvwCourtRoomStatusInfo.Attributes.CssStyle.Add("font-size", "20px");
}
private void BindQueueGrid()
{
List<TestClass> mydata = qService.GetData();
Grid.DataSource = mydata;
Grid.DataBind();
}
You could use a ViewState variable and switch it on each page load. Load the first 10 results on the first load, then on the refresh check the variable and load the second set of results. Mix in a little linq to get your query working.
private void BindQueueGrid()
{
List<TestClass> mydata = qService.GetData();
List<TestClass> onlyTen = new List<TestClass>();
if(ViewState["check"] == null)
{
onlyTen = mydata.Take(10).ToList();
Grid.DataSource = onlyTen;
ViewState["check"] == true;
}
else
{
onlyTen = mydata.Skip(10).Take(10).ToList();
Grid.DataSource = onlyTen;
ViewState["check"] == null;
}
Grid.DataBind();
}
You may be able to do it without the second list but I have not tested the code.
I have a web page that is pulling information about a specific database entry based on the reference number of that database entry. This reference number is not the SQL ID number, but a number that we assign at entry time.
This reference number is passed to the page in the form of a query string, and as long as the reference number actually exists in the database, everything is fine. However, if the reference number does not exist, my details page comes up blank: no exception or anything.
I'm not sure how relevant sharing my code is in this case, but I'll play it safe:
protected void Page_Load(object sender, EventArgs e)
{
using (KidsEntities detailEntities = new KidsEntities())
{
string imgPath = ConfigurationManager.AppSettings["imagePath"];
string KidNum = Request.QueryString["ChildNum"].ToString();
var KidSpecific = from Kid in detailEntities.Kids
where Kid.Number == KidNum
... ;
DescRepeater.DataSource = KidSpecific.ToList();
DescRepeater.DataBind();
}
}
I can put in a redirect in case some joker tries to bring up my details page without going through the main directory (which would bring up a null query string), but if used correctly, my query string will never be null or empty. What I'm trying to prepare for is if someone bookmarks my details page with a query string that was valid at the time of bookmarking, but then gets taken down.
How can I check to make sure there is a reference number in the database that matches the query string before the var "KidSpecific" fires? If there is no such reference number, I need to be able to use a Response.Redirect to put up an error page instead of the blank screen that shows now.
Thanks in advance for any help.
Why do you need to do your check before the query fires? You'll have to check the database for the entry either way. Try redirecting if your query comes up empty:
protected void Page_Load(object sender, EventArgs e)
{
using (KidsEntities detailEntities = new KidsEntities())
{
string imgPath = ConfigurationManager.AppSettings["imagePath"];
string KidNum = Request.QueryString["ChildNum"].ToString();
var KidSpecific = from Kid in detailEntities.Kids
where Kid.Number == KidNum
... ;
var KidSpecificList = KidSpecific.ToList();
//Redirect if there are no results!
if (KidSpecificList.Count() < 1)
Response.Redirect("RedirectPage.aspx");
DescRepeater.DataSource = KidSpecificList;
DescRepeater.DataBind();
}
}
You can check quety string with string.IsNullOrEmpty like this:
protected void Page_Load(object sender, EventArgs e)
{
using (KidsEntities detailEntities = new KidsEntities())
{
string imgPath = ConfigurationManager.AppSettings["imagePath"];
string KidNum = Request.QueryString["ChildNum"].ToString();
if ( string.IsNullOrEmpty ( KidNum ) ) {
Response.Redirect ( "WhatEverURI" );
} else {
var KidSpecific = from Kid in detailEntities.Kids
where Kid.Number == KidNum
... ;
DescRepeater.DataSource = KidSpecific.ToList();
DescRepeater.DataBind();
}
}
}
I've got a pretty simple page, consisting of two DropDownLists populated from the database, and a button. The purpose of the page is pretty simply to allow users to delete an entry from the database. When the button is clicked then a simple LINQ query is executed to delete the intended target, and remove the entry from the dropdownlists, but it doesn't work unless the response is redirected within that function, even if SubmitChanges() was called. Why would this happen?
Edit: Code
protected void Page_Init(object sender, EventArgs e)
{
var result = Database.DB.Data.GetTable<Database.tbl_module_>().Where(module => module.deptCode == ((User)Session["user"]).deptCode);
foreach (var row in result)
{
this.listModuleCode.Items.Add(new System.Web.UI.WebControls.ListItem(row.code));
this.listModuleTitle.Items.Add(new System.Web.UI.WebControls.ListItem(row.title));
}
}
protected void Delete_Click(object sender, EventArgs e)
{
var DB = Database.DB.Data;
var table = DB.GetTable<Database.tbl_module_>();
var result = table.Where(module => module.deptCode == ((User)Session["user"]).deptCode && module.code == listModuleCode.SelectedItem.Text);
listModuleCode.Items.Remove(listModuleCode.SelectedItem);
listModuleTitle.Items.Remove(listModuleTitle.SelectedItem);
table.DeleteAllOnSubmit(result);
DB.SubmitChanges();
Response.Redirect("deletemodule.aspx"); // redirect to this page
}
We need to see your code to help more probably. However:
You need to make sure it knows to delete on submit:
var q = db.Customers.Where(c => c.CustomerID == 2).Single();
db.Customers.DeleteOnSubmit(q);
db.SubmitChanges();
Don't forget you can pass straight SQL to the object:
db.ExecuteCommand("DELETE FROM Customers WHERE ID = 2");
Which you might think is easier.
I have a Drop-Down list in my InfoPath form and I am loading some other fields based on the selection of the drop-down list. so that I have written code as follows for the "changed" event of the drop down list.
public void ProjectName_Changed(object sender, XmlEventArgs e)
{
string projectId = e.NewValue;
dataQueryConnection = (AdoQueryConnection)this.DataConnections["ProjectInformation"];
dataQueryConnection.Command = dataQueryConnection.Command + string.Format(" WHERE ProjectId = '{0}'", projectId);
dataQueryConnection.Execute();
}
For the first time when I change an item in the drop down list its working fine but for the subsequent changes of items(2nd time, etc..) its give the following error,
The query cannot be run for the
following DataObject:
ProjectInformation InfoPath cannot run
the specified query.
[0x80040E14][Microsoft OLE DB Provider
for SQL Server] Incorrect syntax near
the keyword 'WHERE'.
And this is the reason, for the second time,
dataQueryConnection.Command = select
"EmployeeID","Accountname","ProjectName","ProjectId","ProjectRole","BillableUtilization","ClientName","BillingCode","BUHead"
from "TRF"."hrt_vw_ProjectInformation"
as "hrt_vw_ProjectInformation" WHERE
ProjectId = '3072507' WHERE ProjectId
= '3076478'
subsequent event firing biding the WHERE clause every time with the previous executed query.
How I can over come from this issue?
Store the initial command string in a global variable in your code (in the loading event). Then in your Changed function append to the global variable instead of to the previous value of the command.
string OrigString
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
OrigString = dataQueryConnection.Command;
}
public void ProjectName_Changed(object sender, XmlEventArgs e)
{
string projectId = e.NewValue;
dataQueryConnection = (AdoQueryConnection)this.DataConnections["ProjectInformation"];
dataQueryConnection.Command = OrigString + string.Format(" WHERE ProjectId = '{0}'", projectId);
dataQueryConnection.Execute();
}