Solved

Insert on each row

Posted on 2011-03-02
3
278 Views
Last Modified: 2012-08-14
I want to insert a timestamp on each row that is generated in Gridview, Right now I get the date in a separate row,   Can someone tell me where to move the code ? or how can I fix rthis?
private void InsertRecords(StringCollection sc)
    {
        SqlConnection conn = new SqlConnection(GetConnectionString());
        StringBuilder sb = new StringBuilder(string.Empty);
        DateTime datevar = DateTime.Parse(dateLabel.Text.ToString());
        string[] splitItems = null;
                
      
        string sqlStatement2 = "INSERT INTO SampleTable (Date) values('" + datevar + "')";



        foreach (string item in sc)
        {
            //string sqlStatement2 = "INSERT INTO SampleTable (Date) values('" + datevar + "')";
            const string sqlStatement = "INSERT INTO SampleTable (NT1,NT1_Status,NT2,NT2_Status,NT3,NT3_Status,ICU,ICU_Status,SDU,SDU_Status,Tele3rd1,Tele3rd1_Status,Tele3rd2,Tele3rd2_Status,Tele4th1,Tele4th1_Status,Tele4th2,Tele4th2_Status,Pedi3rd,Pedi3rd_Status,Pedi4th,Pedi4th_Status,PICU_SD,PICU_SD_Status,ER,ER_Status,MICU,MICU_Status,CPV,CPV_Status,CVOR,CVOR_Status,CLOF,CLOF_Status,CVOR_HOLDING,CVOR_HOLDING_Status) VALUES";
            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());
           
                sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}','{34}','{35}','{36}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3], splitItems[4], splitItems[5], splitItems[6], splitItems[7], splitItems[8], splitItems[9], splitItems[10], splitItems[11], splitItems[12], splitItems[13], splitItems[14], splitItems[15], splitItems[16], splitItems[17], splitItems[18], splitItems[19], splitItems[20], splitItems[21], splitItems[22], splitItems[23], splitItems[24], splitItems[25], splitItems[26], splitItems[27], splitItems[28], splitItems[29], splitItems[30], splitItems[31], splitItems[32], splitItems[33], splitItems[34], splitItems[35]);
           
            }
            


        }
             
        
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
            SqlCommand cmd2 = new SqlCommand(sqlStatement2, conn);

            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

            cmd.CommandType = CommandType.Text;
            cmd2.ExecuteNonQuery();


            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved');", true);

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            conn.Close();
        }
    }

Open in new window

0
Comment
Question by:TonyReba
  • 2
3 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 35020104
One way would be changing it to :

            const string sqlStatement = "INSERT INTO SampleTable (Date, NT1,NT1_Status,NT2,NT2_Status,NT3,NT3_Status,ICU,ICU_Status,SDU,SDU_Status,Tele3rd1,Tele3rd1_Status,Tele3rd2,Tele3rd2_Status,Tele4th1,Tele4th1_Status,Tele4th2,Tele4th2_Status,Pedi3rd,Pedi3rd_Status,Pedi4th,Pedi4th_Status,PICU_SD,PICU_SD_Status,ER,ER_Status,MICU,MICU_Status,CPV,CPV_Status,CVOR,CVOR_Status,CLOF,CLOF_Status,CVOR_HOLDING,CVOR_HOLDING_Status) VALUES( '" +DateVar+"'";

            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());
           
                sb.AppendFormat("{0}'{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}','{34}','{35}','{36}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3], splitItems[4], splitItems[5], splitItems[6], splitItems[7], splitItems[8], splitItems[9], splitItems[10], splitItems[11], splitItems[12], splitItems[13], splitItems[14], splitItems[15], splitItems[16], splitItems[17], splitItems[18], splitItems[19], splitItems[20], splitItems[21], splitItems[22], splitItems[23], splitItems[24], splitItems[25], splitItems[26], splitItems[27], splitItems[28], splitItems[29], splitItems[30], splitItems[31], splitItems[32], splitItems[33], splitItems[34], splitItems[35]);
           
            }

Open in new window


Additionally you need to comment out or remove this line:

cmd2.ExecuteNonQuery();

The line above this line is wrong. I assume you did not want to set the commandtype for the cmd twice but set it once for cmd and once for cmd2


But the complete structure looks weird...
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35020757
Hi hugo , I am using your suggestion, and i get a error saying sqlStatement must be a constant, do yiou know what could be wrong?

private void InsertRecords(StringCollection sc)
    {
        SqlConnection conn = new SqlConnection(GetConnectionString());
        StringBuilder sb = new StringBuilder(string.Empty);
        DateTime datevar = DateTime.Parse(dateLabel.Text.ToString());
        string[] splitItems = null;
                
      
        //string sqlStatement2 = "INSERT INTO SampleTable (Date) values('" + datevar + "')";



        foreach (string item in sc)
        {
            const string sqlStatement = "INSERT INTO SampleTable (Date, NT1,NT1_Status,NT2,NT2_Status,NT3,NT3_Status,ICU,ICU_Status,SDU,SDU_Status,Tele3rd1,Tele3rd1_Status,Tele3rd2,Tele3rd2_Status,Tele4th1,Tele4th1_Status,Tele4th2,Tele4th2_Status,Pedi3rd,Pedi3rd_Status,Pedi4th,Pedi4th_Status,PICU_SD,PICU_SD_Status,ER,ER_Status,MICU,MICU_Status,CPV,CPV_Status,CVOR,CVOR_Status,CLOF,CLOF_Status,CVOR_HOLDING,CVOR_HOLDING_Status) VALUES( '" + datevar + "'";

Open in new window

0
 
LVL 12

Accepted Solution

by:
HugoHiasl earned 500 total points
ID: 35021205
There are two problems.

I missed a comma at the end of the sqlStatement.

And... this is the error... you are defining sqlStatement as constant which does not allow the variable part of the datevar.

remove the word const in this line:

string sqlStatement = "INSERT INTO SampleTable (Date, NT1,NT1_Status,NT2,NT2_Status,NT3,NT3_Status,ICU,ICU_Status,SDU,SDU_Status,Tele3rd1,Tele3rd1_Status,Tele3rd2,Tele3rd2_Status,Tele4th1,Tele4th1_Status,Tele4th2,Tele4th2_Status,Pedi3rd,Pedi3rd_Status,Pedi4th,Pedi4th_Status,PICU_SD,PICU_SD_Status,ER,ER_Status,MICU,MICU_Status,CPV,CPV_Status,CVOR,CVOR_Status,CLOF,CLOF_Status,CVOR_HOLDING,CVOR_HOLDING_Status) VALUES( '" + datevar + "',";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now