You must Sign In to post a response.
  • Category: [Competition Entries]

    Join operation in webservice from two classes

    Hi, I want to access the data from two tables via join operation in webservice & assign it to Gridview, My codes are working well only for single table. For second table it's showing Null reference exception.I am using the following codes in webservice class where I have declare a variable Name1 of class User type.

    public class Complaint
    {
    int Complaint_ID = 0;
    DateTime Date = default(DateTime);
    string Product_Description = "";
    string Complaint_Description = "";
    string Action = "";
    string Status = "";
    int User_ID = 0;
    User Name1;

    [DataMember]
    public User uname1
    {
    get { return Name1; }
    set { Name1 = value; }
    }
    :
    ......so on
    }
    And here is the class User
    public class User
    {
    string User_ID = "";
    string Name = "";
    string City = "";
    string Address = "";
    string Premises = "";
    string Email_ID = "";
    [DataMember]
    public string name
    {
    get { return Name; }
    set { Name = value; }
    }
    :
    ......so on
    }

    Following are my codes where I want to access the above via SqlDataReader.
    SqlConnection con = new SqlConnection(GetConnectionString());
    con.Open();
    string sel = "select cust.Name,cust.Address,comp.Date, comp.Phone_No,comp.Product_Description,comp.Complaint_Description,comp.Action,comp.Status from Complaint comp join Customers cust on comp.User_ID=cust.User_ID where Complaint_ID='" +this.Textbox1.Text+"'";
    SqlCommand cmd = new SqlCommand(sel,con);
    List<Complaint> cmp = new List<Complaint>();
    SqlDataReader sdr = cmd.ExecuteReader();
    while (sdr.Read())
    {c1.uname1.name = sdr["Name"].ToString();
    c1.uname1.address = sdr["Address"].ToString();
    c1.date = Convert.ToDateTime(sdr["Date"].ToString());
    c1.phoneno = sdr["Phone_No"].ToString();
    c1.productdescription = sdr["Product_Description"].ToString();
    c1.complaintdescription = sdr["Complaint_Description"].ToString();
    c1.action = sdr["Action"].ToString();
    c1.status = sdr["Status"].ToString();
    cmp.Add(c1);
    }
    sdr.Close();
    return cmp;
    }
    Join query is working well in sql white I test, but it's showing error-object reference not set to an instance of an object while I am running it. Please suggest what step I am missing as it's giving NullReference Exception.
  • #756773
    Hi,

    If you are trying to assign null value to object in that case you got this type of error. My suggestion is better to validate the value before assign to object whether the value is null or not based on that condition you should assign the value to object.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #756791
    hi

    refer this url

    http://stackoverflow.com/questions/22341492/check-if-datatable-value-is-null
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/a965a1dd-5cf0-4a2c-b776-28767ea76d85/how-can-i-check-column-is-null-in-datatable?forum=csharpgeneral

    http://stackoverflow.com/questions/2017533/best-way-to-check-if-column-returns-a-null-value-from-database-to-net-applicat

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


  • Sign In to post your comments