1
Answer

How to edit a parsed string? DataGrid?

jeubanks

jeubanks

19y
2k
1

I have a table that stored a question and response to a question.  They are both strings that I would like to build into some type of table for displaying and editing.   The columns and rows are both dynamic.  I'm using C#/Asp 1.1.

The data looks like
header = Name^Address^Phone
response (2 rows) = Jessica^123 Main^555-1212$$Pete^456 Center^666-1212

I can parse out the data without problem.  That isn't the issue.  I need to know the best method to display to information for the user to be able to edit it also.  The number of columns is dynamic as well as the row.  I know I will have to reparse to save as a string.  We is the best method to present this information to the user.  I was trying to use a datagrid but I don't know the number of columns beforehand and they really aren't going to be editing a "row".

Thanks in advance

Answers (1)
0
Vulpes
NA 98.3k 1.5m 12y
You'll need to convert to strings to avoid this problem:

(DateTime.TryParse(maskedTextBox1.Text, out dtparsedDate) ? dtparsedDate.ToString() : "null") 

Notice that it's never necessary to explicitly test whether a bool expression is true.

Notice also that it's not necessary to convert dtParsedDate to a DateTime, as it's a DateTime already. 
Accepted
0
darma teja
NA 493 194.2k 12y
Hi Vulpes, Santhosh,

thanks allot for your fast replies.

If i don't convert null to datetime, it is giving me error.

because:
you can check this one: if i write like this it is giving me error of "can not implicit conversion between system.datetime and null"

(DateTime.TryParse(maskedTextBox1.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : null) 
 

Darma

0
Vulpes
NA 98.3k 1.5m 12y
darma,

If you use Convert.ToDateTime(null) when building your SQL command, then you'll end with a string containing this:

"01/01/0001 00:00:00"

which may be an invalid value for the corresponding column in the database.

Santhosh's point is that you should use "null" instead which should then be translated to NULL in the database assuming (Hemant's point) that the column allows nulls.
0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
Then check whether the columns tyou are tring to insert allow null values. also check whether any integer columns.IIf there are integer columns, then check and do conversion before insert
0
darma teja
NA 493 194.2k 12y
Hi Santhosh,

If user does not enters the date,  it is working well. 

Only my problem with textboxes only.

Darma
0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
Why you are converting null to datetime..Just save it as null. No conversion needed
0
darma teja
NA 493 194.2k 12y
Hi,

Here is my code:

DateTime dtparsedDate = new DateTime();
                string insertsql = "insert into Table1 (ProjectId, ID, PID, Nr, note, Out, Target, Delivery, name,  Out1, Target1, Delivery1, Notes) " +
                                          "values ('" + CmbProjectid.Text + "', '" + ID.SelectedItem + "', '" + textBox1.Text + "', '" + textBox2.Text +
                                          "', '" + textBox3.Text + "', '" + (DateTime.TryParse(maskedTextBox1.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(DBNull.Value)) + "', '" + (DateTime.TryParse(maskedTextBox2.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + (DateTime.TryParse(maskedTextBox3.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) +
                                          "', '" + textBox4.Text + "', '" + (DateTime.TryParse(maskedTextBox4.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + (DateTime.TryParse(maskedTextBox5.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + (DateTime.TryParse(maskedTextBox6.Text, out dtparsedDate) == true ? Convert.ToDateTime(dtparsedDate) : Convert.ToDateTime(null)) + "', '" + textBox4.Text + "');";
                SqlCommand insertcmd = new SqlCommand(insertsql, con1);
                insertcmd.ExecuteNonQuery();

                MessageBox.Show("Data Added Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);


Is user does not enter data in one textbox, than it is giving error.

Whatever user does not enter the data, that should save in the table1 as nulls.


Darma

0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
Show us ur c# code
0
Hemant Kumar
NA 3k 215.1k 12y
You have to alter the table column property as Allow Null =True.Then you can insert the null values also.