0

I'm trying to bind and display 4 concatenate item in dropdownlist using ajax. Like this eg. (127,CoilWt,1,KGS ) one of the value in dropdownlist should appear like this.from database. In database i am selecting

`select CODE_VALUE,CODE_DESC,CODE_SUB_VALUE,CODE_SUB_DESC FROM TCODE

html part

<td><select class='form-control' id='Certific'><option value='' disabled='disabled' selected='selected'>Please select a name</option></select></td>

script part

$(function () {
                        $.ajax({
                            type: "POST",
                            url: "TDC.aspx/GetCertificate",
                            data: '{}',
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (r) {
                                var Certific = $("[id*=Certific]");
                                Certific.empty().append('<option selected="selected" value="0">Please select</option>');
                                $.each(r.d, function () {
                                    Certific.append($("<option></option>").val(this['Value']).html(this['Text']));
                                });
                            }
                        });
                    });

c# side

public class GetCertificate
{
    public int ID { get; set; }
    public string Code_Desc { get; set; }
}

 [WebMethod]
    public static List<GetCertificate> GetCertificate()
    {

        string connStr = ConfigurationManager.ConnectionStrings["conndbprodnew"].ToString();
        OracleConnection objconn = new OracleConnection(connStr);
        string prop_name, tdc_property = "", qry = "";
        qry = "SELECT CODE_DESC from tdc_product1 ";
        OracleCommand objFetchCmd = new OracleCommand(qry, objconn);
        List<GetCertificate> Certificate = new List<GetCertificate>();
        objconn.Open();        
        OracleDataReader ReadData = objFetchCmd.ExecuteReader();
        while (ReadData.Read())
        {

            GetCertificate.ID = ReadData["ID"].ToString();
            GetCertificate.CODE_DESC = ReadData["CODE_DESC"].ToString();

        }
        return Certificate;
    }

Where is the mistake i am trying like this but getting error at GetCertificate.ID .Any idea would be appreciated.

hari
  • 103
  • 2
  • 12

1 Answers1

0

I guess you're making mistake at:

GetCertificate.ID = ReadData["ID"].ToString();
GetCertificate.CODE_DESC = ReadData["CODE_DESC"].ToString();

GetCertificate seems to be a type not a instance of object.

You should try something like:

Certificate.Add(new GetCertificate { ID = ReadData["ID"].ToString(), CODE_DESC = ReadData["CODE_DESC"].ToString() } );

Please be aware that I wrote this without any IDE, so there could be typo/syntax error, but you get the idea.

Small hint: Of course there're plenty of room for code refactor in your code (e.g. rename Certificate to Certificates), but that is another topic.

krs
  • 543
  • 2
  • 17
  • but if i want to display concatenate value in each row of dropdown like this (127,CoilWt,1,KGS) then how to do. – hari Jan 29 '18 at 12:09
  • Easiest way to do concatenation is through Linq. In your case I would do the following: 1. Override GetCertificate's class ToString() method to return ID.ToString() + "," + Code_Desc. 2. In GetCerficate() method instead of returning list, use Linq to produce concatenated string. Something like [Certificate.Aggregate((p, n) => p.ToString() + "," + n.ToString())](https://stackoverflow.com/questions/217805/using-linq-to-concatenate-strings). – krs Jan 29 '18 at 12:25