Extract the value of an Item from a DropDownList that is filled with a sql server C # query and aspx

0

I have a DropDownList called Sale that I fill in as follows:

 DataRow fila = dtr.Rows[0];
 OdbcCommand cmd = new OdbcCommand("SELECT A.CardCode, A.CardName, B.SlpName FROM OCRD A INNER JOIN OSLP B ON A.SlpCode = B.SlpCode WHERE B.SlpCode ='" + Convert.ToString(fila["SlpCode"]) + "' AND LicTradNum = 'XAXX010101000'", cn);
 OdbcDataAdapter daa = new OdbcDataAdapter(cmd);
 DataTable ds = new DataTable();
 daa.Fill(ds);
 VENTA.DataSource = ds;     
 VENTA.DataTextField = "CardName";
 VENTA.DataValueField = "CardCode";
 VENTA.DataBind();

I assign the CardCode to each Item as value . My intention is to show CardCode in a Label, depending on the Item that was selected and this through the Click event of a Button.

    
asked by Elizabeth 29.06.2018 в 19:28
source

1 answer

2

I recommend using parameters instead of concatenating your query

OdbcCommand cmd = new OdbcCommand("SELECT A.CardCode, A.CardName, B.SlpName  FROM OCRD A INNER JOIN OSLP B ON A.SlpCode = B.SlpCode WHERE B.SlpCode =@slpcode AND LicTradNum = 'XAXX010101000'", cn);

cmd.Parameters.AddWithValue("@slpcode",Convert.ToString(fila["SlpCode"]);

Because your error is in the query, you can also try to do the same query in sql server to see if it works

    
answered by 29.06.2018 / 23:49
source