473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Missing first parameter exception.

I am new to VsC#.
Am working on Visual Studio .NET Pro 2003.

I wrote a solution that is a form containing 2 buttons.
Button1 uses folderBrowserDialog to select a directory.
Button2 uses saveFileDialog to get a file name.

Then the file names from the directory selected are then written to the text
file.

The problem: When user dosen't select a directory and cancels out of the
folderBrowserDialog and then presses button2 and selects a text file it
thorws an exception.

What is the best way to handle this and catch the error? Here's the main code:

static void Main()
{
Application.Run(new Form1());

}
private string folderName1, fileName1 ;

private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;
}
}

private void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

DialogResult result2 = saveFileDialog1.ShowDialog();
if( result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderName1);
StreamWriter writer = new StreamWriter(fileName1);
foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
{
writer.WriteLine(" {0}\\{1}", file.DirectoryName, file.Name);
}
writer.Close();
Application.Exit();
}
}
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

TIA

Bill
Nov 17 '05 #1
5 3488
folderName1 is going to be empty which means that when you attempt to use it
you will have problems. You can check before you do anything with the
"save" dialog box if folderName1 is actually set like this:

if (folderName1.Length == 0) {
Application.MessageBox("Please use the first button");
return;
}

Alex

"BillZondlo" <Bi********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
I am new to VsC#.
Am working on Visual Studio .NET Pro 2003.

I wrote a solution that is a form containing 2 buttons.
Button1 uses folderBrowserDialog to select a directory.
Button2 uses saveFileDialog to get a file name.

Then the file names from the directory selected are then written to the
text
file.

The problem: When user dosen't select a directory and cancels out of the
folderBrowserDialog and then presses button2 and selects a text file it
thorws an exception.

What is the best way to handle this and catch the error? Here's the main
code:

static void Main()
{
Application.Run(new Form1());

}
private string folderName1, fileName1 ;

private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;
}
}

private void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

DialogResult result2 = saveFileDialog1.ShowDialog();
if( result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderName1);
StreamWriter writer = new StreamWriter(fileName1);
foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
{
writer.WriteLine(" {0}\\{1}", file.DirectoryName, file.Name);
}
writer.Close();
Application.Exit();
}
}
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

TIA

Bill

Nov 17 '05 #2
Hi Bill,

Have you considered setting the Enabled property to false for Button2 and
only setting that to true after Button1 is successful?
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;
// and then add something like:
if (folderName1.Length > 0)
button2.Enabled = true; }
}


In your button2_Click you might also test the directory exists:
if (System.IO.Directory.Exists (folderName1))
and only then create the DirectoryInfo
Cheers,
Steve Goodyear
Vancouver, Canada
Nov 17 '05 #3
Hi there... you should verify that folderName1 != null. For example:

if( result2 == DialogResult.OK && folderName1 != null)

You can also write less code if you do something like this...

if ( saveFileDialog1.ShowDialog().Equals(DialogResult.O K) &&
folderBrowserDialog1.SelectedPath != null)

There's no need to have two members to store information about the dialogs
your're using because you can get this from their respective properties.

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda

"BillZondlo" <Bi********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
I am new to VsC#.
Am working on Visual Studio .NET Pro 2003.

I wrote a solution that is a form containing 2 buttons.
Button1 uses folderBrowserDialog to select a directory.
Button2 uses saveFileDialog to get a file name.

Then the file names from the directory selected are then written to the
text
file.

The problem: When user dosen't select a directory and cancels out of the
folderBrowserDialog and then presses button2 and selects a text file it
thorws an exception.

What is the best way to handle this and catch the error? Here's the main
code:

static void Main()
{
Application.Run(new Form1());

}
private string folderName1, fileName1 ;

private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;
}
}

private void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

DialogResult result2 = saveFileDialog1.ShowDialog();
if( result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderName1);
StreamWriter writer = new StreamWriter(fileName1);
foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
{
writer.WriteLine(" {0}\\{1}", file.DirectoryName, file.Name);
}
writer.Close();
Application.Exit();
}
}
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

TIA

Bill

Nov 17 '05 #4
Thanks Alex & Steve.

So many ways, it seems, to solve probelms.

Better to look for some directions before making spagetti.

Bill

"Steve Goodyear" wrote:
Hi Bill,

Have you considered setting the Enabled property to false for Button2 and
only setting that to true after Button1 is successful?
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;


// and then add something like:
if (folderName1.Length > 0)
button2.Enabled = true;
}
}


In your button2_Click you might also test the directory exists:
if (System.IO.Directory.Exists (folderName1))
and only then create the DirectoryInfo
Cheers,
Steve Goodyear
Vancouver, Canada

Nov 17 '05 #5


"Angel J. Hernández M." wrote:
Hi there... you should verify that folderName1 != null. For example:

if( result2 == DialogResult.OK && folderName1 != null)

You can also write less code if you do something like this...
if ( saveFileDialog1.ShowDialog().Equals(DialogResult.O K) &&
folderBrowserDialog1.SelectedPath != null)


Ah, I was thinking along these lines....

Thanks

Bill

There's no need to have two members to store information about the dialogs
your're using because you can get this from their respective properties.

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda

"BillZondlo" <Bi********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
I am new to VsC#.
Am working on Visual Studio .NET Pro 2003.

I wrote a solution that is a form containing 2 buttons.
Button1 uses folderBrowserDialog to select a directory.
Button2 uses saveFileDialog to get a file name.

Then the file names from the directory selected are then written to the
text
file.

The problem: When user dosen't select a directory and cancels out of the
folderBrowserDialog and then presses button2 and selects a text file it
thorws an exception.

What is the best way to handle this and catch the error? Here's the main
code:

static void Main()
{
Application.Run(new Form1());

}
private string folderName1, fileName1 ;

private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;
}
}

private void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

DialogResult result2 = saveFileDialog1.ShowDialog();
if( result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderName1);
StreamWriter writer = new StreamWriter(fileName1);
foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
{
writer.WriteLine(" {0}\\{1}", file.DirectoryName, file.Name);
}
writer.Close();
Application.Exit();
}
}
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

TIA

Bill


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Herring | last post by:
Hi there: Well, let me start off by saying that I am a Visual Studio drag and drop weenie, not a real programmer. So I tend to get confused when things do not look like the instructions said they...
1
by: N S S | last post by:
I Get the following Error ================ Error =============================== Procedure 'GetCetgoriesOrProducts' expects parameter '@CategoryID', which was not supplied. Description: An...
6
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get...
2
by: Andrew | last post by:
Hey all, Have a strange one here, and being still fairly new to .NET isn't helping me understand it. I am having a problem where a DataReader doesn't return all the rows when I try to use a...
0
by: JIM.H. | last post by:
Hello, Here is my code, first part of this code is adding a static record to an SQL table and working fine, second part should get the data from the parameter myDSDest and put into the same SQL...
4
by: Joe HM | last post by:
Hello - I realize that there is no more IsMissing function in VB.NET but how can I have a boolean argument that is optional and in the code I need to determine whether it was passed it or not? ...
0
by: Sean Howard | last post by:
I have a strange problem linking tab delimited text files in Access 2000 (I am running Windows XP), please try this and let me know if I am going mad. Step 1. Create the tab-delimited text...
0
by: Tifer | last post by:
Hello, I am building my first .Net Application. The first couple of Publish and Installs I did went fine. But after a couple of builds, I get a modal dialogue box error every time upon trying...
6
by: JDS | last post by:
I want to be able to use effectively a table adaptor query that can take several arguments with several of those arguments possibly null. I have not been able to do this elegantly. When I first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.