Pergunta

my problem is that , i made a child form for searching , but i have problem in sql query and parameters , my code is

SqlConnection sc = new SqlConnection(
    "Data Source=MOHAMMED-PC;Initial Catalog=salessystem;Integrated Security=True");

SqlCommand command = new SqlCommand(
    "Select * from customers WHERE (docno = @doc) OR (NAME LIKE @name ) OR (salepoint = @salepoint)", sc);
DataTable dt = new DataTable();
command.Parameters.AddWithValue("@doc", doctxt.Text);
command.Parameters.Addwithvalue("@name", nametxt.Text);
command.Parameters.AddWithValue("@salepoint", salepointtxt.Text);
SqlDataAdapter sda = new SqlDataAdapter(command, sc);
sda.Fill(dt);
dataGridView1.DataSource = dt;

i have error in sql adapter command and in where clause command , any help ??

Foi útil?

Solução

Three things:

  1. You have a typo on this line

    command.Parameters.Addwithvalue("@name", nametxt.Text);
    

    The method is AddWithValue (note the case difference)

  2. The constructor of SqlDataAdapter takes the command but no connection then since the command already contains the connection, so this is correct:

    SqlDataAdapter sda = new SqlDataAdapter(command);
    

    Probably the most important last:

  3. If you use LIKE you need to use the wild-cards %:

    command.Parameters.AddWithValue("@name", string.Format("%{0}%",nametxt.Text);
    
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top