-2

I am new in Dapper. When user selects an Item-name in drop-down style Text-box and click Select Button , then that particular row should be added in datagridview.

Problem is that txt_sell_item.Text does not retrive value and add it in datagridview. Instead if i just write ItemName='bread' in my query it works. ('bread' is an item in my ItemName Column in database)

[ I also bind my datagridview with Order Class]

My Select Button Click Event is.

    using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
    {
         if (db.State == ConnectionState.Closed)
            db.Open();
            string query = "SELECT *FROM tbl_ItemDetail WHERE ItemName='{txt_sell_item.Text}'";
            ordersBindingSource.DataSource = db.Query<Orders>(query, commandType: CommandType.Text);
            dataGridView1.Refresh();  
    }

My Order Class is...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    public class Orders
    {
        public int    ItemId      { get; set; }
        public string ItemName    { get; set; }
        public string Brand       { get; set; }
        public string Category    { get; set; }
        public int    Quantity    { get; set; }
        public int    Price       { get; set; }
    }
}

My GUI is

Umair Mahmood
  • 63
  • 1
  • 9

1 Answers1

2

This has nothing to do with Dapper, but it is an error on your query string.
You should write it as (just adding the initial $ to understand your txt_sell_item.Text)

string query = $"SELECT * FROM tbl_ItemDetail WHERE itemName='{txt_sell_item.Text}'";

However you don't concatenate together strings to build sql command but you need to use a parameterized query.

So the query text becomes

string query = "SELECT *FROM tbl_ItemDetail WHERE ItemName=@ItemName";

and the command is

ordersBindingSource.DataSource = db.Query<Orders>(query, new {ItemName = txt_sell_item.Text});
Steve
  • 213,761
  • 22
  • 232
  • 286