Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
The below datas in datagridview as follows;

Date Name

1/3/2012 Ram,suresh


in the database i want to save as follows;

Date Name
1/3/2012 Ram
1/3/2012 suresh


how to split the string with comma and save in the database.

for saving the two datas into the database how can i do using csharp.

note it is windows application.
Posted

I believe it's best to split your string into three elements, and extract the date:
C#
private string test = @"1/3/2012 Ram,suresh";

private char[] splitChars = new[] {',', ' '};

private void testData()
{
    var splitString = test.Split(splitChars);

    if(splitString.Length(!= 3)) // throw error ?

    DateTime date;

    if (DateTime.TryParse(splitString[0], out date))
    {
        // you have a valid date
    }
    else
    {
        // throw error ?
    }

    // update database
    // date splitString[1] => database
    // date splitString[2] => database
 
Share this answer
 
v2
Comments
Richard Deeming 27-Nov-14 14:08pm    
This is a question from March 2013! It only appeared in the list because a site-driving spammer posted solution #3 to drive traffic to his blog.
BillWoodruff 27-Nov-14 17:29pm    
Thanks for the heads-up, Richard. I did not realize it was an old question. Do you think having another answer here is a bad thing ? cheers, Bill
Richard Deeming 28-Nov-14 8:25am    
I think the general consensus is that you should avoid answering old questions, unless there are exceptional circumstances. Eighteen months after the question was asked, and particularly with two accepted solutions, the OP has either solved their problem or moved on to something else.

The danger here is that, once Solution 3 has been deleted as site-driving spam, it looks like you're the person who's resurrected an old solved question. :)
Follow the below steps:

1. you have to split the string by ',' using C# and store in string array.
2. Use foreach loop with the string array insert the data into database.

For Example:

C#
string strValue = "Ram,suresh";
string[] strArray = strValue.Split(',');

foreach (object obj in strArray)
{
//your insert query
}
 
Share this answer
 
Comments
Member 11361173 3-Sep-15 9:02am    
thanks
C#
Datetime strDate;
strDate="1/3/2012";
string strText="Ram,suresh";
string []strArr=strText.split(',')

for(i=0;strArr.Length;i++)
{
strSQl='insert into tablename(datet,textvalue) values ('"+strDate+"','"+ strArr[i].Tostring() +"');'   

}


Please ignore the cases and use parameterized query.

updated, sorry for my mistake, thanks

Thanks
 
Share this answer
 
v2
Comments
Prakash Thirumoorthy 28-Feb-13 23:38pm    
is it correct syntax? strText.split[',']. or its correct?. strText.split(',')
[no name] 1-Mar-13 0:26am    
strText.split(',') is correct.
Rahul Rajat Singh 2-Mar-13 0:06am    
The split part seems to be ok but why are you assigning the string inside the loop. You should have the string ready before the loop and you can put the values inside that string inside the loop.
AshishChaudha 2-Mar-13 2:00am    
The data should be saved in database like following
Date Name
1/3/2012 Ram
1/3/2012 suresh
C# Split() handles splitting upon given string and character delimiters. The listing below shows how to use the string Split() and Join() methods:

C#
// separate individual items between commas
            string[] year = commaDelimited.Split(new char[] {','});

// combine array elements with a new separator
            string colonDelimeted = String.Join(":", year);


for more details http://www.stupidcodes.com/joining-and-splitting-strings-in-c-net

Thanks
 
Share this answer
 
Comments
CHill60 13-Jan-15 5:40am    
Do not post solutions to old questions that are already resolved. Particularly with site-driving spam. Your account may be disabled if you continue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900