Click here to Skip to main content
15,888,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I am doing result search and in that there are many fields and many records. Now i am able to index all the records and search all the columns and fields. I want to search only the particular column or field. Can you please help me out.
Posted
Updated 21-Apr-16 1:13am
v2
Comments
RickZeeland 21-Mar-16 7:35am    
You probably already know about it, but here is an article about Lucene.NET: http://www.codeproject.com/Articles/320219/Lucene-Net-ultra-fast-search-for-MVC-or-WebForms

1 solution

The error message tells you what's wrong: The stored procedure expects a parameter @Booknum which you did not supply with your SqlCommand. Instead you add an unnamed SqlParameter with no value - which doesn't help because the stored procedure expects that named parameter. Also, you need to provide the SqlParameter before you execute the SqlCommand (by calling ExecuteReader() on it).

It might work if you do this:
C#
cmd.Parameters.Add(new SqlParameter());           // remove this line
cmd.Parameters.AddWithValue("@Booknum", Booknum); // (note the quotes)
SqlDataReader reader = cmd.ExecuteReader();       // line moved


Edit: You should move this line:
C#
con.Close();

..below the if-block in which it currently is in:
C#
if (reader.HasRows)
{
   // ...
}
con.Close();

..because you want to close the SqlConnection in any case, not only if there are no results.

Even better would be to use the SqlConnection with a using-Statement because this will ensure that the connection gets closed (and disposed) in any case, even if an exception is thrown somewhere "inbetween" - and the same goes for all other Sql****-objects like the SqlCommand, SqlDataReader, SqlTransaction (and others):
C#
using (SqlConnection con = new SqlConnection(connstring))
using (SqlCommand cmd = new SqlCommand(sproc, con))
{
    con.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Booknum", Booknum);

    using(SqlDataReader reader = cmd.ExecuteReader())
    {
        List<Document> docs = new List<Document>();
 
        if (reader.HasRows)
        {
            // ...
        }
    }

    // no explicit closing of the connection required any more
}
 
Share this answer
 
v3
Comments
Sascha Lefèvre 21-Mar-16 8:21am    
Which error do you get if you declare it like this?:
public void Index(string indexDir, string connstring, string sproc, string Booknum)
Sascha Lefèvre 21-Mar-16 9:34am    
Double-click on the first error that you've shown here and take a look at the code. You obviously attempt to provide a string where some method expects a generic list (of a type that I don't know because the error message doesn't tell).

Also: In your test case the declaration of the SqlParameter appears to be completely unneccessary. But what I just spotted there: Is @Booknum supposed to be an integer (because you use SqlDbType.Int there)? Then you might want to change string Booknum in your Index-method to int Booknum.

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