Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure Inserts Twice

Status
Not open for further replies.

gouber82

Programmer
Joined
Oct 12, 2003
Messages
5
Location
US
I have a web page that I want to insert keywords into a database when they do a search. The problem is when a search is done, the keyword is inserted. When another search is done both the old keyword and a new keyword are inserted. I have the following stored procedure and code ...


CREATE PROCEDURE dbo.sp_addKeywords
@keywords varchar(100)
AS

Insert into searchRequests (keywords) values (@keywords)
RETURN
GO

*************************

SqlConnection cn2 = new SqlConnection(Configuration.DBConnectionString);
SqlCommand cmd2 = new SqlCommand("sp_addKeywords", cn2);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("@keywords", SqlDbType.VarChar, 100).Value = keywords;
cn2.Open();
SqlDataReader result2 = cmd2.ExecuteReader(CommandBehavior.CloseConnection);
result2.Close();
cn2.Close();

I have found lots of discussions about this, just no sure answer.

Thanks,
TJ
 
Make sure that you reset the variable that holds the keyword in your VB code. If you don't reset the variable you just keep appending to it.

Code:
SqlConnection cn2 = new SqlConnection(Configuration.DBConnectionString);
SqlCommand cmd2 = new SqlCommand("sp_addKeywords", cn2);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("@keywords", SqlDbType.VarChar, 100).Value = keywords;
cn2.Open();
SqlDataReader result2 = cmd2.ExecuteReader(CommandBehavior.CloseConnection);
result2.Close();
cn2.Close();

Set keywords = null



"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top