Selected value in asp.net dropdown in a repeater not returned - asp.net

I've currently go a very strange problem. I'm using an asp.net wizard to upload some files. The files are uploaded using plupload. After the files have uploaded I have a list of the upload files stored in a session variable. I use the session variable to create a table showing the upload files. The user now has option to set a file category using a dropdown in the table. When the user clicks 'finish' button the code reads the list of files and the category from the table. The odd thing is this code works fine on my development machine and on several servers but on a particular clients server the drop down value always returns as null. Here is the relevent code:
protected void Page_Init(object sender, EventArgs e)
{
bindRepeater();
}
private void bindRepeater()
{
ArrayList sessionFiles = (ArrayList)Session["PLUploadFiles"];
IList<document> files = new List<document>();
foreach (string fileName in sessionFiles)
{
document doc = new document();
doc.FileName = fileName;
doc.Description = fileName.Split('.').First();
files.Add(doc);
}
TableRepeater.DataSource = files;
TableRepeater.DataBind();
}
protected void SaveButton_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in TableRepeater.Items)
{
Label descriptionLabel = (Label) item.FindControl("DescriptionLabel");
String description = descriptionLabel.Text;
Label fileNameLabel = (Label)item.FindControl("FileNameLabel");
String fileName = fileNameLabel.Text;
DropDownList categoryDropDown = (DropDownList) item.FindControl("CategoryDropDownList");
string category = categoryDropDown.SelectedValue;
if(SaveClicked != null)
{
SaveEventArgs s = new SaveEventArgs();
s.FileName = fileName;
s.Category = category;
s.Description = description;
SaveClicked(this, s);
}
}
Response.Redirect(RedirectURL);
}
Note that the entire wizard lives on a usercontrol. Has anybody got any idea why this code works fine on most machines but fails on one particular server?

Looks like I've fixed it. For some reason I was seeing a double postback. This was calling my code to reset the session variables in Page_Load at the wrong time. The work around is to reset the session variables in the page that links to the upload page so they are reset before the page is loaded. I've no idea why I'm seeing the double postback.

Related

Can't change RootFolder of ASPxFileManager control using Callback method

I'm using DevExpress 13.1 to develop my web application. My page has two controls: A Gridview which contains some item and a FileManager control (is children of a callbackpanel) which contain files information of item which is focused on that Gridview. I'm using Gridview's FocusRowChange client event to get data and send it back to server through an callback (of callback panel) to set new RootFolder value, but it not works. Tell me where's my wrong?
Thanks in advance.
My code:
ASPX file:
function myGridView_FocusRowChanged(s, e) {
var index = s.GetFocusedRowIndex();
var soCT = s.GetRowValues(index,"SoChungThu;SoHopDong",LoadFileList);
}
function LoadFileList(values) {
myCallbackPanel.PerformCallback("CHANGE_ROOT_FOLDER" + "," + values);
}
CS file:
protected void myCallbackPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
{
var vals = e.Parameter.Split(',');
if (vals[0].ToUpper() == "CHANGE_ROOT_FOLDER")
{
var path = vals[0] + "/" + vals[1];
myFileManager.Settings.RootFolder = path;
}
}
}
I found a solution.
Store folder path in Session, and assign it to RootFolder in Page_Load event.
Thank for reading.

How do I check a not-null query string against a sql database in c# for ASP.NET?

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();
}
}
}

Create a newEventHandler for a button. But Data fields are empty for some reason

I create a new event Handler to handle two different events. One is for saving a new document. The other is for saving an edit.
I added this in my Page_load:
if (Request.QueryString["ExhibitID"] != null)//new
{
if (!IsPostBack)
{
ddlCaseFiles.DataSourceID = "dsCaseFiles";
ddlCaseFiles.DataTextField = "Display";
ddlCaseFiles.DataValueField = "FileID";
rbByFileID.Checked = true;
rbMyFiles.Checked = false;
ddlCaseFiles.DataBind();
editExhibit(int.Parse(Request.QueryString["ExhibitID"]));//new
exhibitHeader.InnerText = "Edit Exhibit";
}
hidSavedExhibitID.Value = Request.QueryString["ExhibitID"];
saveExhibitBtn.Click += new EventHandler(this.btnUpdateExhibit_Click);
}
else
{
saveExhibitBtn.Click += new EventHandler(this.saveExhibitBtn_Click);
}
my save method for some reason keeps looping then crashing because the second time it goes through, there is no data since I reset it after the first save. I have no idea why it is running my save method twice.
this is my save method :
protected void saveExhibitBtn_Click(object sender, EventArgs e)
{
hidSavedExhibitID.Value = null;
int newExhibitID = saveExhibit();
int propertyID = autoCreateProperty(newExhibitID);
linkExhibitAndProperty(newExhibitID, propertyID);
SaveInfoIntoSessionVariables();
ClearFormFields();
}
the "saveExhibit()" method is where I actually access the DB and store everything. It works fine.
Because you re bind your datas in your Page_Load.
You must persist your datas with ViewState, EnableViewState="true"
You bind your datas just one time, in the ! IsPostBack. in order to not erase the selected values
If(! IsPostBack)
{
//Bind your datas with `DataBind()`
}

Can't submit changes to database or page controls

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.

Problem in dynamically load data to Drop-Down list box in InfoPath 2007?

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();
}

Resources