|
ps. index += 1 equals index = index + 1
|
|
|
|
|
Yeah, I know, which is why I was so confused by it.
|
|
|
|
|
Hi,
I am trying to transfer a txt file by loading it and saving it with a save file dialog.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.AddExtension = true;
saveDialog.FileName = "Checkers Game";
saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
saveDialog.OverwritePrompt = true;
saveDialog.Title = "Save game";
saveDialog.ValidateNames = true;
saveDialog.ShowDialog();
if (saveDialog.FileName != "")
{
writer.Close();
file.Close();
FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
fs = (System.IO.FileStream)saveDialog.OpenFile();
}
but when i go to the saved file i get an empty file even though the text i loaded isn't empty.
Thanks
|
|
|
|
|
I don't see any statement where you actually write the text to the file. It looks like the statements in the 'if' clause are in the wrong order and incomplete.
|
|
|
|
|
the statements in the if clause close a different stream. The one which i want to open with the new stream. Google came up with
FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
for opening txt files.
|
|
|
|
|
What is wrong with File.Copy ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
I don't know the destination file. he user desides where he wants to save it with the save dialog.
|
|
|
|
|
bar3000 wrote: I don't know the destination file
You are using the SaveFileDialog and you don't know the destination file name?
|
|
|
|
|
Hi,
your code seems completely wrong to me. Here are some of the problems:
- a SaveFileDialog is meant for the user to enter a new filename or choose an existing filename, or to cancel the intended operation. To do that correctly you should compare the return value of ShowDialog() against DialogResult.OK (you compare FileName with empty string, I doubt that is equivalent) and use the FileName value as the path of the output file (you don't use the value at all).
- (the part shown of) your code does not declare, create or use writer and file except for closing it, so at best something gets written somewhere else, and some (which?) file gets closed; anyway changing the state of writer and file should not be a side effect of the saveDialog.FileName != "" test, the writer and the file operation should only exist inside the code block following that test, and nowhere else.
- declaring a FileStream and opening a file inside the code block of the if statement, while not using that stream, does not make any sense. Once the if-block is done, the stream is out of scope, hence useless.
- if your code creates a FileDialog, it should also dispose of it; the using statement is the easiest way of doing it right.
I suggest you have a look at some CodeProject articles; a lot of them describe small applications that do load and save some data from and to a file, using OpenFileDialog and SaveFileDialog. Just use these as search terms in the CP search facility.
If you don't fully understand the FileDialog class, read its documentation; if you still feel uncomfortable, go buy and study an introductory book on the language of your choice. It will teach you the basics in a systematic way.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:38 AM
|
|
|
|
|
You can use this code at your own risk. I wrote it without being really awake.
using (SaveFileDialog saveDialog = new SaveFileDialog())
{
saveDialog.AddExtension = true;
saveDialog.FileName = "Checkers Game";
saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
saveDialog.OverwritePrompt = true;
saveDialog.Title = "Save game";
saveDialog.ValidateNames = true;
if (saveDialog.ShowDialog() == DialogResult.OK)
{
File.Copy(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", saveDialog.FileName, true);
}
}
|
|
|
|
|
Code looks good, however spoon feeding is bad.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:39 AM
|
|
|
|
|
The FileDilaog classes only allow the user to select a path, they don't actually do anything with files.
An easy way to actually write or open a file is using the Filename property of the OpenFileDialog and SaveFileDialog and save that in a string variable.
Then you use File.ReadAllText() method to read a file and File.WriteAllText() method to write the text to a file.
These work even without giving the user a possibility to specify the location or name of the file.
Both methods have a string parameter called path , that's where you can put the path you just got from the FileDialogs (or a fixed path).
modified on Thursday, February 26, 2009 8:11 AM
|
|
|
|
|
Hello new member here on The Code Project but I have used the site many times to find answers for my programing questions but so far have had no luck finding an answer for my current issue.
As the title very simply says I have a program that contains alot of RTF fields (20+) all of which are placed right next to each other vertically. Everything appears correctly except that between a couple of the RTF fields there is a gap (maybe 1px tall) were two of them meet running the length of the field.
Although this is a minor issue it is one that I must solve.
Any help would be greatly appreciated. If more information is needed please feel free to ask and thank you in advance.
|
|
|
|
|
You're placing them on a form using code, or..? You should be able to just re size the fields left of the gap or set the location of the fields on the right side 1 pixel to the left.
I don't know anything about using RTF fields in C#, so maybe I'm way off in my understanding?
|
|
|
|
|
Thank you for the reply, the RTF fields are placed on the form using the designer. We have tried to shift them 1 pixel at a time but find then that they overlap by 1 pixel! In designer mode they ARE next to each other.
I understand that RTF fields are not used very often anymore (I'm actually not the programmer but I know enough to tell our programmer may have a few screws loose) but any ideas would still be appreciated.
|
|
|
|
|
|
I am creating a Cache Manager that will call a GetCacheItem method to get either a DataTable in cache or if the DataTable is not in cache the GetCacheItem method will make the SQL database call and then put the returned DataTable into cache. The parameters I want to pass to the GetCacheItem method are the cache item key and a delegate of type Func<tresult> with a delegate name of QueryExpression. The problem is that I want the Func<t> QueryExpression delegate to be used by several SQL select statments each with a different number of query parameters. How can I implement the Func<t> delegate to be able to take a different number of parameters each time it is called? Below is an example of what I have so far:
public object GetCacheItem(String sCacheKey, Func<object> QueryExpression)
{
object objCacheItem = null;
if (!CachingEnabled)
{
return objCacheItem;
}
if (CacheItem<object>(sCacheKey) != null)
{
return CacheItem<object>(sCacheKey);
}
else
{
lock (syncObject)
{
if (CacheItem<object>(sCacheKey) != null)
{
return CacheItem<object>(sCacheKey);
}
else
{
Object cacheItem = QueryExpression();
AddToCache<object>(sCacheKey, cacheItem);
return cacheItem;
}
}
}
}
</object></object></object></object></object></object>
NOTE: As you can see my Func<t> delegate only has a return argument and not the list of variable parameters. What do I need to do?
Thanks,
Steve
|
|
|
|
|
NOTE:
I used a dictionary to do this in calls before. The problem is that my boss wants to be able to make SQL calls like the following:
DAC_MANAGER.GetCustomers(string sName, string sAge)
and
DAC_MANAGER.GetEmployees(string id, string position, string payRate)
I talked to him about using dictionaries and he does not want to because this would mean changing every method in the DAC layer to use a dictionary.
|
|
|
|
|
helo... i have a client application which has a tcp listener on port 1000 and a server which connects to the same port.but the server should connect first to the client so when i try to connect Exception occurs like client not accepting the connection.
the cliet is listening in the netstat -an on the port 1000 and in the server when i send then the connection to the client is SYN_SENT and not established... will the windows firewall block these connections ..?
|
|
|
|
|
Yes.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
and if the client connects to the server first then will the firewall block the incomming connecton to the client ?
|
|
|
|
|
|
Hi I have a problem i hgaave a table in data grid view and i am updating data from data using a button. what i need to do is once the user update the data and click on update button i want to convert all string values to lower case. i am not using data table to populate my grid and i am updating using stored procedure. so i dnt know how to convert changed values to Lowercase before updating. can any one help me please.
Thanks
|
|
|
|
|
Hi I have a problem i hgaave a table in data grid view and i am updating data from data using a button. what i need to do is once the user update the data and click on update button i want to convert all string values to lower case. i am not using data table to populate my grid, is their any way that i can do that here is my code i am using to upgrade my grid
private void cmd_update_record_Click(object sender, EventArgs e)
{
if (dgRepair.RowCount == 0)
{
MessageBox.Show("No data available for update");
}
else
{
DataSet ChangedRepDataSet = new DataSet();
ChangedRepDataSet = ds_rep.GetChanges();
if (ChangedRepDataSet != null)
{
// Intializing object of Connectionclass to open connection
conn_obj = new ConnectionClass();
store_connection = conn_obj.connect();
// Sqlcommand object
SqlCommand cmdupdate;
cmdupdate = new SqlCommand();
cmdupdate.CommandType = CommandType.StoredProcedure;
cmdupdate.CommandText = "Update_Status_Grid";
//opening connection
cmdupdate.Connection = store_connection;
store_connection.Open();
//Adding Parameters for Repair table
cmdupdate.Parameters.Add("@OrgDescription", SqlDbType.VarChar, 100, "Description");
cmdupdate.Parameters["@OrgDescription"].SourceVersion = DataRowVersion.Original;
cmdupdate.Parameters.Add("@Description", SqlDbType.VarChar, 100, "Description");
cmdupdate.Parameters.Add("@OrgStatus", SqlDbType.VarChar, 15, "Status");
cmdupdate.Parameters["@OrgStatus"].SourceVersion = DataRowVersion.Original;
cmdupdate.Parameters.Add("@Status", SqlDbType.VarChar, 15, "Status");
cmdupdate.Parameters.Add("@OrgPartDes", SqlDbType.VarChar, 50, "PartDes");
cmdupdate.Parameters["@OrgPartDes"].SourceVersion = DataRowVersion.Original;
cmdupdate.Parameters.Add("@PartDes", SqlDbType.VarChar, 50, "PartDes");
cmdupdate.Parameters.Add("@OrgRepAction", SqlDbType.VarChar, 50, "RepAction");
cmdupdate.Parameters["@OrgRepAction"].SourceVersion = DataRowVersion.Original;
cmdupdate.Parameters.Add("@RepAction", SqlDbType.VarChar, 50, "RepAction");
cmdupdate.Parameters.Add("@OrgId", SqlDbType.BigInt ,1000, "Id");
cmdupdate.Parameters["@OrgId"].SourceVersion = DataRowVersion.Original;
//Adding the Parameters for invoice table
cmdupdate.Parameters.Add("@OrgRepCost", SqlDbType.Money, 1000, "RepCost");
cmdupdate.Parameters["@OrgRepCost"].SourceVersion = DataRowVersion.Original;
cmdupdate.Parameters.Add("@RepCost", SqlDbType.Money, 1000, "RepCost");
cmdupdate.Parameters.Add("@OrgPaymentPaid", SqlDbType.Money, 1000, "PaymentPaid");
cmdupdate.Parameters["@OrgPaymentPaid"].SourceVersion = DataRowVersion.Original;
cmdupdate.Parameters.Add("@PaymentPaid", SqlDbType.Money, 1000, "PaymentPaid");
da_rep.UpdateCommand = cmdupdate;
if (check == true)
{
try
{
da_rep.Update(ds_rep, "Repair");
ds_rep.AcceptChanges();
}
catch (Exception ex)
{
MessageBox.Show("Their is some problem in updating make sure you are entering right values " +
ex);
}
}
//}
//}
}
else
{
MessageBox.Show("no changes made");
}
//int modifiedRows = da_cus.Update(myChangedDataset);
//MessageBox.Show("Database has been updated successfully:" + no_of_rows_chang + " rows has been changed");
}
can any one help me
Thanks
|
|
|
|
|
First can you please use "CODE Block" tag from now-onwards, for your code. its really hard to read the code without that. If I understand correctly then,
Insterad of
cmdupdate.Parameters["@OrgStatus"].SourceVersion = DataRowVersion.Original;
You can use
cmdupdate.Parameters["@OrgStatus"].SourceVersion = DataRowVersion.Original.ToString().ToLower();
Whenever you are storing string values just use .ToString().ToLower() .
|
|
|
|