StreamWriter sWrite = new StreamWriter("Data.txt", true);
string strWrite = txtID.Text + ","
+ txtName + ","
+ txtAdress;`enter code here`
sWrite.WriteLine(strWrite);
sWrite.Close();
MessageBox.Show("Person Added");
this is my code, why the output looks like this?
(System.Windows.Forms.TextBox, Text: Yasser,System.Windows.Forms.TextBox, Text: Jeddah,202)
>Solution :
When you want to write the contents of a TextBox to a string, you need to use the TextBox.Text property to access the content.
In your attempt you have used the Text property of the txtID TextBox control correctly, but not the others. The default output of any object when you concatenate it into a string is to call the .ToString() method on that object, by default this will return the name of the type of that object. TextBox however overrides this base and returns the name of the type as well as the content of the default property which is the .Text property. That is why we see "System.Windows.Forms.TextBox, Text: Jeddah" in the output.
We do not need to assume that those controls are textboxes, this output confirms it for us. Therefore, we would expect the following code to produce output similar to your expectations:
string strWrite = txtID.Text + ","
+ txtName.Text + ","
+ txtAdress.Text;
I can’t explain why your example output text is in the reverse order though, so I doubt that this output matches the code you have posted as it is.