Duplicating rows in database
For some reason, the if statement below is not working
''# count if records with this user already exist in the database below
objSQLCommand = New SqlCommand("select count(id) as record_count from table1 where user = @strUser", objSQLConnection)
objSQLCommand.Parameters.Add("@strUser", SqlDbType.VarChar, 3).Value = strUser
objSQLCommand.Connection.Open()
intRecordCount = CType(objSQLCommand.ExecuteScalar(), Integer)
objSQLCommand.Connection.Close()
''# duplicate default rows in database if username not in database
If intRecordCount = 0 Then
Response.Write(intRecordCount)
objSQLCommand = New SqlCommand("insert into table1 (heading, user) select heading, @strUser as user from table1 where defaults = @intDefault", objSQLConnection)
objSQLCommand.Parameters.Add("@strUser", SqlDbType.VarChar, 3).Value = strUser
objSQLCommand.Parameters.Add("@intDefault", SqlDbType.Int, 4).Value = 1
End If
Response.Write(intRecordCount) returns a 0 if 0 records are f开发者_Go百科ound and it returns 2 if i manually insert 2 rows for the user in the database.
But for some reason, the if statement does not work. If I run the "insert select" query manually, it works perfectly.
What am I doing wrong?
You are not executing the second SQL insert command as you do in:
intRecordCount = CType(objSQLCommand.ExecuteScalar(), Integer)
精彩评论