Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,
i am stuck with a question where i have two tables client and company.
company table has column id and company
client has three column client_id , client_name and company_id

on page i have two text box one says company and other is client , textbox which takes values for client is multiline textbox (which is a need here).

everything is done, insert is working fine, delete is working fine but while updating i have question how to delete those client which are stored in the table client but after updating it they should not be there.



C#
public void isclientpresent(int selectecustomer, string clientname)
 {
     SqlConnection con = new SqlConnection(@"Data Source=AASHISH\SQLEXPRESS;Initial Catalog=customerdb;Integrated Security=True");
     con.Open();
     SqlCommand cmd = new SqlCommand("Select * from client where company_id='"+selectecustomer+"'and client_name='"+clientname+"'", con);
     SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
     DataSet sqlds = new DataSet();
     sqlda.Fill(sqlds);
     con.Close();

     if (sqlds.Tables[0].Rows.Count > 0)
     {
         //
     }
     else
     {
         insertclient(selectecustomer, clientname);
     }

 }



protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
     int selectedcustomer = Convert.ToInt16(GridView1.Rows[e.RowIndex].Cells[3].Text);

     string[] allLines = txtclients.Text.Split('\n');
     foreach (string text in allLines)
     {
         isclientpresent(selectedcustomer, text);
     }

 }

 public void insertclient(int selectedcompany, string selectedclient)
 {
     SqlConnection con = new SqlConnection(@"Data Source=AASHISH\SQLEXPRESS;Initial Catalog=customerdb;Integrated Security=True");
     con.Open();
     SqlCommand cmd = new SqlCommand("Insert into client (client_name,company_id) VALUES('" + selectedclient+ "',"+selectedcompany+")", con);
     cmd.ExecuteNonQuery();
     con.Close();

 }


What I have tried:

When i select update GridView1_RowUpdating is shooted and one by one make to database to check if they exist there or not . if not then they call insert query.

but what if value is there is database but not sent from array to check .

Can anyone help me on this
Posted
Updated 17-Aug-16 7:11am
Comments
Vincent Maverick Durano 17-Aug-16 12:59pm    
adding to SQL Injection, I would also suggest you to always use the "using block" when dealing with objects that eat resources, such as: SqlConnection and SqlCommand

1 solution

Quote:
delete those client which are stored in the table client but after updating it they should not be there.


You just need to re-bind your GridView to reflect the changes you've made. So when you Update or Delete data, do something like this:

C#
private void BindGrid(){          
      using (SqlConnection sqlConn = new SqlConnection
            (ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString)){
                string sql = "SELECT * FROM YourTableName";
                using(SqlCommand sqlCmd = new SqlCommand(sql,sqlConn)){
                    sqlConn.Open();
                    using(SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd)){
                        sqlAdapter.Fill(dt);
                    }
                }
            }

            if(dt.Rows.Count > 0){
                GridView1.DataSource = dt;
                GridView1.DataBind();
            } 
}


then at RowUpdating and RowDeleting events, you simply call the method BindGrid() to refresh your Grid with the changes:

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){
   //Your existing code
   BindGrid();
}



Please do take note of the following:

(1) Store your connection string in your web.config, rather than hard-coding it in your code behind.

(2) Always use Parameter Queries when passing parameters to your SQL query. Here's another reference that you may want to look at: Protect Your Data: Prevent SQL Injection[^]

(3) Remember to use the "Using Block" when dealing with objects that eat resources, such as: SqlConnection and SqlCommand
 
Share this answer
 
v2

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