Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Inserting data repeatedly in database table when every am clicking insert button.

can anyone give me solution .

C#
ServiceController[] services = ServiceController.GetServices();
{
              dataGridView1.DataSource = services ;
}
 

 
 string conn = "Data Source=HostName;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
            SqlConnection mycon = new SqlConnection(conn);
            mycon.Open();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    
                    string Displayname = row.Cells[3].Value.ToString();
                    string ServiceName = row.Cells[4].Value.ToString();
                    string Status = row.Cells[5].Value.ToString();
                    string ServiceType = row.Cells[6].Value.ToString();              
                    
    
                    SqlCommand com = new SqlCommand(conn, mycon);
                   
                    com .CommandText = "insert into SystemServiceInfo(DisplayName,ServiceName,Status,ServiceType)values('" + Displayname + "','" + ServiceName + "','" + Status + "','" + ServiceType + "')";
                    
                   com.ExecuteNonQuery();
                    com.Parameters.Clear();
                    com.Dispose();
 

                }
            
                mycon.Close();
                mycon.Dispose();


Thanks & Regards
sam.198979
Posted
Comments
_Damian S_ 29-May-13 1:10am    
Not sure what your question is... could you please rephrase.

1 solution

Hello Sam,

You probably will have to write a piece of code which checks if the values inserted don't exist in the Database table i.e. SystemServiceInfo in your case.

If the values exist you can skip the Insert Query and prompt with a message.You will have to match the full set of values.

Hope this helps.

Rgds
Himanshu
 
Share this answer
 
Comments
sam.198979 5-Jun-13 6:15am    
any sample code ll be helpful

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