i am new to this so hope u guys bear with me . i am trying to insert into database the URL directory of the filePathImage upon btnDone.
Partial Codes:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
EnsureDirectoriesExist();
String filepathImage = (#"Images/Story/" +txtTitle.Text + "/" + e.FileName);
AjaxFileUpload1.SaveAs(Server.MapPath(filepathImage));
}
protected void btnDone_Click(object sender, EventArgs e)
{
act.ActivityName = dropListActivity.SelectedItem.Text;
act.Title = txtTitle.Text;
act.FileURL = filepathImage;
daoStory.Insert(act);
daoStory.Save();
}
i got a problem with filePathImage in act.FileURL = AjaxFileUpload1.filePathImage; Any advise or solutions will be grateful
try below, when upload complete you can put your path in to session and take that session path when you needed.
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
// your Code
Session["filepathImage"] = filepathImage ; // put the path in session variable
}
protected void btnDone_Click(object sender, EventArgs e)
{
if(Session["filepathImage"]!=null)
{
string filepathImage = Session["filepathImage"] as string;
// your code ...
}
}
Related
When i store an item, i can store it and read it. But when i try to store another item, the first one disappear.
I'm using a masterpage for my default site dont know if it affects it.
my code is very simple and looks like this.
List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
Session["kurv"] = kurv;
}
In my masterpage it looks like this
List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
Repeater1.DataSource = kurv;
Repeater1.DataBind();
}
Because you have a bug - you declare the same name outside and inside the Page_load, so on the click the outside is used - so you actually use two different lists.
I write your code adding 1 & 2 to understand the bug.
List<kurvliste> kurv_1 = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv_2 = (List<kurvliste>)Session["kurv"];
if (kurv_2 == null)
{
kurv_2 = new List<kurvliste>();
Session["kurv"] = kurv_2; // Store the new list in the session object!
}
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv_1.Add(new kurvliste(1,1,1, "Produktnavn"));
Session["kurv"] = kurv_1;
}
How to solve it
But if i remove the outside, i get a null reference just by leaving it
to be List kurv.
No you need to remove the inside of Page_Load declaration that hides the outside one
protected void Page_Load(object sender, EventArgs e)
{
// on this line, just remove the declaration
// List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
// do it like
kurv = Session["kurv"] as List<kurvliste>;
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
}
Alternative
I suggest to move the list on the session to a property like that
// using this const you avoid bugs in mispelling the correct key.
const string ckurvlisteNameConst = "kurvliste_cnst";
public List<kurvliste> kurv
{
get
{
// If not on the Session then add it
if (Session[ckurvlisteNameConst] == null)
Session[ckurvlisteNameConst] = new List<kurvliste>();
// this code is not exist on release, but I check to be sure that I did not
// overwrite this Session with a different object.
Debug.Assert(Session[ckurvlisteNameConst] is List<kurvliste>);
return (List<kurvliste>)Session[ckurvlisteNameConst];
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
}
a similar answer : How to store list of object into ViewState
Now solved. Thanks for your answers!
This is my code right now:
//Listbox scripts is the name of my folder
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + #"Listbox scripts"))
{
string file2 = file.Split('\\').Last();
listBox1.Items.Add(file2);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("SetText", new object[]
{
File.ReadAllText(string.Format("./Listbox scripts/{0}", listBox1.SelectedItem.ToString()))
});
}
I'm new to coding in C# and I have a textbox that has the names of text files in a directory and when I click on the text file in the listbox it's supposed to load the text from it into my textbox (named 'ScriptBox')
Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
string User = System.Environment.MachineName;
textBox1.Text = "{CONSOLE} Welcome to Linst, " + User + "!";
directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + #"Scripts");
files = directory.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName); //these parts are the parts that dont work
}
Thanks in advance!
Add the below into your Form1.cs. What this is going to do is when a user clicks a listbox item, its going to call (raise an event) the "listBox1_MouseClick" method and set the text of the textbox to the text of the listbox item. I just quickly created an app and implemented the below and it works.
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.Text;
}
And add the below to the Form1.Designer.cs where the rest of your list box properties are. The below is subscribing to an event, the listBox1_MouseClick method in Form1.cs, so when a user clicks on a listbox item, the listBox1_MouseClick method is going to run.
this.listBox1.MouseClick += new MouseEventHandler(this.listBox1_MouseClick);
I hope the above makes sense.
Your code is nice, and perfect but it just need a little validation check in list index selection
Try thing in your listbox_selectedIndexChanged
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex!=-1)
{
FileInfo selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName);
}
}
I actually don't see a problem with your code, could it be a typo somewhere?
I did this and it worked for me:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in System.IO.Directory.GetFiles(#"c:\"))
{
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
textBox1.Text = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
}
}
I am trying to make a program for the car retail firm I work at. When people deliver their cars, a guy has to take pictures of the damages with a Lenovo miix.
The program I've made so far is not smart enough.
It goes like, you write the numberplate in a box, then create a folder with the text from the box.
Then you start the camera, take a picture and save it, and manually have to find the folder and name the file.
Is there a way I can make it smarter with just 2 buttons, one to start the camera and another one to take the picture, and then it automatically saves it in a folder named as the numberplate, and files named 1,2,3,4 and so on.jpg ?
this is my code so far:
namespace Europcar_skade_camera
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;
private void Form1_Load(object sender, EventArgs e)
{
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(FilterInfo VideoCaptureDevice in webcam)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
}
comboBox1.SelectedIndex = 1;
}
private void button1_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
cam.Start();
}
private void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bit;
}
private void button3_Click(object sender, EventArgs e)
{
if (cam.IsRunning)
{
cam.Stop();
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = #"C:\tmp\";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void nummerplade_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(#"c:\tmp\" + nummerplade.Text);
}
}
}
Something like this:
Instead of using a SaveFileDialog, you could just create the file path based on the number plate value like this:
var filePath = System.IO.Path.Combine(#"c:\tmp", nummerplade.Text, fileNumber + ".jpeg");
The fileNumber var is the number of files allready in the folder + 1.
var fileNumber = Directory.GetFiles(...).Length + 1
string numberPlate = "21323412";
string path = #"C:\tmp\";
Directory.CreateDirectory(path + numberPlate); //to create folder
string dirpath = path + numberPlate; //to get name of fodler we created
You also mentioned that you want incremental names, there are many ways of doing it, personally i'd prefer large hashed name of file like so:
//generate GUID and convert to string.
string filename = Guid.NewGuid().ToString();
//to save picturebox image in folder and use ImageFormat.Your desired format
pictureBox1.Image.Save(dirpath + #"\"+filename+".jpeg", ImageFormat.Jpeg);
But if you want to stick with incremental names:
// you get last name in the folder:
//it gets last filename in folder but with path
var lastFilePath = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();
//we remove path from filename and convert it to int.
int lastIndex = Int32.Parse(Path.GetFileNameWithoutExtension(lastFilePath));
//then we increment it by 1
int newName = lastIndex + 1;
//and save it
pictureBox1.Image.Save(dirpath + #"\"+filename+".jpeg", ImageFormat.Jpeg); //to save picturebox image in folder
With GUID finally it should look something like this:
string numberPlate = "21323412";
string path = #"C:\tmp\";
private void button2_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(path + numberPlate); //to create folder
string dirpath = path + numberPlate; //to get name of fodler we created
string filename = Guid.NewGuid().ToString();
pictureBox1.Image.Save(dirpath + #"\"+filename+".jpeg", ImageFormat.Jpeg); //to save picturebox image in folder
}
I set up a session variable on the Booking form.aspx as so:
protected void confirmImageButton_Click(object sender, ImageClickEventArgs e)
{
Session["confirmBooking"] = "confirm";
Session["beachBach"] = beachBachRadioButtonList.SelectedItem.Text;
}
and I transfer to my other page as so:
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
I'm trying to add 1 to the beachBach session variable whenever user press the confirm button....however, when i start to debug it, instead of adding 1, it add 2 to the label.
Can someone please help me out.. thanks
try this
protected void Page_Load(object sender, EventArgs e) {
{
if(!IsPostBack)
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
Well what happens after we come to this page ?? Do we have any event that causes postback in this page??
If there is than definitely it will add 1 again to your session .
Try using !IsPostBack property .
Help me ..my page index is not working in visual studio.
my page load is as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}
}
protected void CustomerView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
int newPageNumber = e.NewPageIndex + 1;
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}
what am i doing wrong my page index in not working.
Try this. I think you have to set the GridView's PageIndex property manually.
protected void CustomerView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
CustomerView.PageIndex = e.NewPageIndex;
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}