Click here to Skip to main content
15,922,574 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
cmd.CommandText = "INSERT INTO AD_DETAIL (CAT_ID, SUB_CAT_ID, AD_TITLE, AD_DESC, AD_PRICE, AD_IMG, BUY_SELL_ID,DATE,AD_POST_DATE,DELETED_AD) VALUES (" + ddlCountry.SelectedValue + "," + ddlState.SelectedValue + ",'" + txtTitle.Text + "','" + txtDesc.Text + "','" + txtPrice.Text + "','" + "Images/" + uniquefilename + extension + "','" + rbPost.SelectedValue + "', @date,@postdate,0)";

cmd.Parameters.AddWithValue("@date", date.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@postdate", datepost.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + uniquefilename);
cmd.ExecuteNonQuery();

cmd.CommandText = "INSERT INTO SELLER_DETAIL (SELLER_NAME, SELLER_EMAIL_ID, SELLER_MOB_NO, SELLER_DEP, SELLER_FLOOR,EMP_ID) VALUES ('" + txtName.Text + "','" + txtEmailID.Text + "'," + txtContact.Text + ",'" + txtDep.Text + "','" + txtFloor.Text + "','" + txtEmpNo.Text + "')";
Posted
Updated 22-Apr-14 13:18pm
v3
Comments
[no name] 22-Apr-14 18:55pm    
First format your code so that it is readable.
Second, use parameterized queries. Do not mix parameterized queries and sql injection attack queries. It defeats the purpose.
Third, you are executing the query twice.
Harshit Wadhera 22-Apr-14 19:13pm    
sir i have update the code.. when i insert values in second table (SELLER_DETAILS)it adds the same data twice.
[no name] 22-Apr-14 19:21pm    
Yes. Your code is still unformatted. You are still mixing parameterized queries with SQL Injection attack queries. And even though you removed it, I would bet that you are still executing the query twice.
Harshit Wadhera 22-Apr-14 19:29pm    
yes i am executing the query twice because there are two tables in which i have to insert the data.
[no name] 22-Apr-14 19:36pm    
No.... you are executing the queries 3 times (according to your first code example). Once for the AD_DETAIL table and TWICE for the SELLER_DETAIL table.

Why are you trying insert data two table with two times from front end.
Easily you can write a 
1.Storeprocedure with required parameters
2.Writed both of Insert query inside the storeprocedure
3.Call only storeprocedure one time with single connection 
4.It will insert to both of your table.
 
Share this answer
 
try creating new instance of cmd after executing the first query ... something like

cmd = new SqlCommand() ;
 
Share this answer
 
Comments
Harshit Wadhera 22-Apr-14 19:10pm    
i have tried this but still not working.
Hi Try like this in first query u use cmd.CommandText and second query also cmd.CommandText so try to declare different cmd(like cmd,cmd1)or if u execute first query u need to dispose the cmd ,i think cmd as sqlcommand ? is it if u use same object u need to dispose like cmd.Dispose() then again u assign the query to cmd.And one this in each function or each query u need to use dispose,close connection then only it not affect in other place,what u try above in same function u try to execute 2 queries with same sqlcommand object,so u have dispose or declare new name cmd.




C#
cmd.CommandText = "INSERT INTO AD_DETAIL (CAT_ID, SUB_CAT_ID, AD_TITLE, AD_DESC, AD_PRICE, AD_IMG, BUY_SELL_ID,DATE,AD_POST_DATE,DELETED_AD) VALUES (" + ddlCountry.SelectedValue + "," + ddlState.SelectedValue + ",'" + txtTitle.Text + "','" + txtDesc.Text + "','" + txtPrice.Text + "','" + "Images/" + uniquefilename + extension + "','" + rbPost.SelectedValue + "', @date,@postdate,0)";
 
cmd.Parameters.AddWithValue("@date", date.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@postdate", datepost.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + uniquefilename);
cmd.ExecuteNonQuery();
 

//Here u use cmd.Dispose(); or declare new cmd1 and use in second query.

cmd.CommandText = "INSERT INTO SELLER_DETAIL (SELLER_NAME, SELLER_EMAIL_ID, SELLER_MOB_NO, SELLER_DEP, SELLER_FLOOR,EMP_ID) VALUES ('" + txtName.Text + "','" + txtEmailID.Text + "'," + txtContact.Text + ",'" + txtDep.Text + "','" + txtFloor.Text + "','" + txtEmpNo.Text + "')";

Regards
Aravind
 
Share this answer
 

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