Hello,.. Im a New Guy to this forum and 1st of all;... Hi :twisted:

im a newbie to c# programming as well ,,, anyway.. i have a huge issue ..
here is what s going to happen..

if (txtPath.Text != "")
                {
                    FileInfo fn = new FileInfo(txtPath.Text);
                    string p = txtPath.Text;
                    int pos = p.LastIndexOf(".");
                    string ext = p.Substring(pos + 1);
                    string newname = mid + "." + ext;
                    string path = Application.StartupPath.ToString() + "\\images\\" + newname;
                    fn.CopyTo(path);

                    sql = "INSERT INTO mobile(MID,MMNO,MPRICE,MINFO,MMANU,MPIC,MDOA) VALUES('" + mid + "','" + mmno + "','" + mprice + "','" + minfo + "','" + manu + "','" + newname + "','" + doa + "')";
                    dbcon.Open();
                    com = new OleDbCommand(sql, dbcon);
                    com.ExecuteNonQuery(); // executing the query ( sending the wehical )
                    MessageBox.Show("Successfully Inserted", "Record Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dbcon.Close();
                    picImage.ImageLocation = txtPath.Text;
                    txtPath.Clear();
                    FormClear();
                    txtMid.Focus();
                }

using the above coding.. im uploading a image from my form to a folder in my application root.. the coding works fine... i must mention that im using ole-db as well in here to send data to the db /..

what happens is when i close this form after uploading the image and stuff.. the startup path is changed to the folder where i chose a image.. :P

ex - c:/users/myimages

and the software seeks the .Database in the directory where i chose a image to upload,,, ( in my example it would be c:/users/myimages ) when i reopen the same form the application path has changed :confused:

so my question is ,,,

is their a way to reset the application path ?

Tnx in advance ;)

Welcome to the forum!
You have a couple options here. You can use reflection to get the executing path of the assembly. Or since you are new at it. I would just save that startup path to a string class variable. they use that from then on out. best place to do that is in the constructor.

public class Form1 : Form
 {
     private string StartPath = "";

     public Form1()
     {
       StartPath = Application.StartupPath;
     }

     ...
  }

Then just use that variable instead.

it ddint work out :(

i tired it.. but still the error comes,, the path is being reset after i upload a image.. and the changed path is the folder where i chose the image to upload....

Any Ideas ? to rest the location ?

i don't think you followed me, if Application.StartupPath gives you the dir you need the first time you call it. Then you can save that path to a string. and then from then on out use that string instead of the Application.StartupPath.

The reason why it "changes" is because the working directory changes when you change it in a open or save file dialog box. But that doesn't matter because when the program first run you saved that data to a variable that WON'T change unless you manually change it.

its a class variable, available to the entire form.

If it didn't work, post the code that doesn't work. I will tell you why.

it ddint work out :(

i tired it.. but still the error comes,, the path is being reset after i upload a image.. and the changed path is the folder where i chose the image to upload....

Any Ideas ? to rest the location ?

i did ,, it still ddnt work out,, anyway ill post the original project source before i modified it

using System.IO;
using System.Data;
// importing the OLEDB thing :P
using System.Data.OleDb;
using System.Windows.Forms;

namespace Login_Form
{
    public partial class frmAddMobile : Form
    {
        public frmAddMobile()
        {
            InitializeComponent();
        }

        // Global Areas Stuff - U Knw :D :D bt dnt ask me haridhaa :D ------------------------------------------------------

        // Global vars - new
        OleDbConnection dbcon;
        OleDbCommand com;
        string sql;

        // Creating the Onload event
        private void frmAddMobile_Load(object sender, EventArgs e)
        {
            string dbloc = Application.StartupPath.ToString() + "\\G2.mdb";
            dbcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+dbloc);
        }

        // Method Used to Clear The Text Fields
        private void FormClear()
        {
        }

        // Button & Combo Box Coding Stuff -----------------------------------------------------------------------


        private bool checkid(string id)
        {
           // i removed this code to make code look smaller ;)
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            if (txtMid.Text == "")

                // i removed if statements to make the coding bit smaller

            else
            {
               
                string mid, mmno, mprice, minfo, manu;
                DateTime doa;

                mid = txtMid.Text;
                mmno = txtModelNo.Text;
                mprice = txtPrice.Text;
                minfo = txtInfo.Text;
                manu = cmbManu.Text;
                doa = DateTime.Now;

                bool id = checkid(mid);

                if (id == false)
                {
			// removed if to make code look smaller ;)
                }

                if (txtPath.Text != "")
                {
                    FileInfo fn = new FileInfo(txtPath.Text);
                    string p = txtPath.Text;
                    int pos = p.LastIndexOf(".");
                    string ext = p.Substring(pos + 1);
                    string newname = mid + "." + ext;
                    string path = Application.StartupPath.ToString() + "\\images\\" + newname;
                    fn.CopyTo(path);

                    sql = "INSERT INTO mobile(MID,MMNO,MPRICE,MINFO,MMANU,MPIC,MDOA) VALUES('" + mid + "','" + mmno + "','" + mprice + "','" + minfo + "','" + manu + "','" + newname + "','" + doa + "')";
                    dbcon.Open();
                    com = new OleDbCommand(sql, dbcon);
                    com.ExecuteNonQuery(); // executing the query ( sending the wehical )
                    MessageBox.Show("Successfully Inserted", "Record Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dbcon.Close();
                    picImage.ImageLocation = txtPath.Text;
                    txtPath.Clear();
                    FormClear();
                    txtMid.Focus();
                }

                else
                {
                    MessageBox.Show("Please Select A Image From The DB", "What the Heck Are u Doin Men ?");
                }

            }
        }

    }
}

AS i mentioned, this is the non modified coding ;)

my purpose is to reset the application path after the data has being submitted to the Database.. i must mention this too.. i ddnt get any sort of errors in the code after using the dbloc var in winodws 7 and vista. but still the error occurs in Windows Xp although i updated the .net and SQL versions in it :|

Thanks alot for the quick response :) ill be waiting for a reply ;)

OK. you need to create a Class variable that is of string type. I will edit this source and post it back. there is no need to "reset" the startuppath.

using System.IO;
using System.Data;
// importing the OLEDB thing :P
using System.Data.OleDb;
using System.Windows.Forms;

namespace Login_Form
{
    public partial class frmAddMobile : Form
    {
        public frmAddMobile()
        {
            InitializeComponent();
			
			//Set start up path here.
			StartPath = Application.StartupPath;
        }

        // Global Areas Stuff - U Knw :D :D bt dnt ask me haridhaa :D ------------------------------------------------------

        // Global vars - new
        OleDbConnection dbcon;
        OleDbCommand com;
        string sql;
		
		//create string here in class to hold startuppath.
		string StartPath = "":

        // Creating the Onload event
        private void frmAddMobile_Load(object sender, EventArgs e)
        {
			//use startpath string instead of Application.StartupPath;
            string dbloc = StartPath + "\\G2.mdb";
            dbcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+dbloc);
        }

        // Method Used to Clear The Text Fields
        private void FormClear()
        {
        }

        // Button & Combo Box Coding Stuff -----------------------------------------------------------------------


        private bool checkid(string id)
        {
           // i removed this code to make code look smaller ;)
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            if (txtMid.Text == "")

                // i removed if statements to make the coding bit smaller

            else
            {
               
                string mid, mmno, mprice, minfo, manu;
                DateTime doa;

                mid = txtMid.Text;
                mmno = txtModelNo.Text;
                mprice = txtPrice.Text;
                minfo = txtInfo.Text;
                manu = cmbManu.Text;
                doa = DateTime.Now;

                bool id = checkid(mid);

                if (id == false)
                {
			// removed if to make code look smaller ;)
                }

                if (txtPath.Text != "")
                {
                    FileInfo fn = new FileInfo(txtPath.Text);
                    string p = txtPath.Text;
                    int pos = p.LastIndexOf(".");
                    string ext = p.Substring(pos + 1);
                    string newname = mid + "." + ext;
				    //use startpath string instead of Application.StartupPath;
                    string path = StartPath + "\\images\\" + newname;
                    fn.CopyTo(path);

                    sql = "INSERT INTO mobile(MID,MMNO,MPRICE,MINFO,MMANU,MPIC,MDOA) VALUES('" + mid + "','" + mmno + "','" + mprice + "','" + minfo + "','" + manu + "','" + newname + "','" + doa + "')";
                    dbcon.Open();
                    com = new OleDbCommand(sql, dbcon);
                    com.ExecuteNonQuery(); // executing the query ( sending the wehical )
                    MessageBox.Show("Successfully Inserted", "Record Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dbcon.Close();
                    picImage.ImageLocation = txtPath.Text;
                    txtPath.Clear();
                    FormClear();
                    txtMid.Focus();
                }

                else
                {
                    MessageBox.Show("Please Select A Image From The DB", "What the Heck Are u Doin Men ?");
                }

            }
        }

    }
}

Now it should work fine :)
(I didn't check any of your code, I just replaced the parts that apply to the startup path)

Thanks.. I Did the something after reading your previous post ( tutorial post ).. and now i copied the source you modified as well , , it still gives errors in winxp :( i think its a bug of win xp or in my notebook :) , but it works fine in windows 7 and vista :)

anyway thanks for helping me out :) Thanks in advance sir :) You Rock :cool:

It could possibly be the .net version you have installed on the XP machine. but you might try using the another method that uses reflection.

just replace this line

StartPath = Application.StartupPath;

with this one

StartPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

if it doesn't work on xp after that. It might be the .net version or a problem with your laptop. its all good code.

It could possibly be the .net version you have installed on the XP machine. but you might try using the another method that uses reflection.

just replace this line

StartPath = Application.StartupPath;

with this one

StartPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

if it doesn't work on xp after that. It might be the .net version or a problem with your laptop. its all good code.

Thanks alot sir... it was with the version of the .net :)
i had to install 3.5 to get it working ...

sir... when i create a setup for it, can i embed the setup of .net and sql with it so that the users will no longer experience this sort of errors .. Thanks in advance :)

you can't embed the .net setup. but you can freely share the .net redistributable. But don't worry. if you use visual studio to create a .msi installer package, it will automatically make sure you have the required .net version installed and if not, it will prompt to install it from Microsoft.

you can't embed the .net setup. but you can freely share the .net redistributable. But don't worry. if you use visual studio to create a .msi installer package, it will automatically make sure you have the required .net version installed and if not, it will prompt to install it from Microsoft.

Thank You Sir :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.