Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All ,
How can I get values from database into the textboxes on my aspx page. So far I've done this:-

C#
using (SampleDataContext dbContext = new SampleDataContext())
       {
           try
           {
               var queryuserinfo = (from User_Information in dbContext.User_Informations
                                    where User_Information.UserName == Session["MatriUser"].ToString()
                                    select new
                                    {
                                        id = User_Information.ID,
                                        username = User_Information.UserName,
                                        firstname = User_Information.FirstName,
                                        lastname = User_Information.LastName,
                                        email = User_Information.Email,
                                        phone = User_Information.PhoneNo,
                                        location = User_Information.Location,
                                        createdby = User_Information.Created_By,
                                        gender = User_Information.Gender,
                                        dtob = User_Information.dob,
                                        region = User_Information.region,
                                        lang = User_Information.lang
                                    }).ToList();

               IEnumerable<info>myinfo

           }

           catch
           {
               Response.Redirect("login.aspx");
           }


I don't know what to do next please help....
Posted
Comments
Maciej Los 6-Jan-16 13:00pm    
You should provide more details about data binding...
Improve your question and copy-paste .cs and .aspx page content.
Deepak Kanswal Sharma 7-Jan-16 20:02pm    
My aspx page is really a long one so I can't post it here. I've given the .cs code which is applied on page_load.

Assuming this works, there are a couple of issues.
1. queryuserinfo is a list of anonymous items. This is because of the select new statement. I think your intention is to have an info class to select into. - select new info... then queryuserinfo would be a list of info objects similar to myinfo.

Example:
C#
var queryuserinfo = (from User_Information in dbContext.User_Informations
                                    where User_Information.UserName == Session["MatriUser"].ToString()
                                    select new info
 
Share this answer
 
Comments
Deepak Kanswal Sharma 2-Jan-16 23:39pm    
var queryuserinfo = (from User_Information in dbContext.User_Informations
where User_Information.UserName == Session["MatriUser"].ToString()
select new info
{
id = User_Information.ID,
username = User_Information.UserName,
firstname = User_Information.FirstName,
lastname = User_Information.LastName,
email = User_Information.Email,
phone = User_Information.PhoneNo,
location = User_Information.Location,
createdby = User_Information.Created_By,
gender = User_Information.Gender,
dtob = User_Information.dob,
region = User_Information.region,
lang = User_Information.lang
}).ToList();

IEnumerable<info>myinfo

I tried this, but it's giving me error 'The type or namespace name 'info' could not be found'
info needs to be a class you define. I could only assume that was already done because code would not compile on your line

C#
IEnumerable<info>myinfo
</info>


Since it is defining type of info.

Anyways, here are some references for you to learn from:


https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

and Linqpad examples:

LINQPad examples - Home[^]

see Linqpad:

https://www.linqpad.net

 
Share this answer
 
Comments
VICK 4-Jan-16 1:29am    
Please use "Improve solution" to edit your previously posted answer and updated with more details, rather posting a new solution every time.
I got the solution somehow:-

C#
var queryuserinfo = (from userinfo in dbContext.User_Informations
                                           join reg in dbContext.registers on userinfo.UserName equals reg.UserName
                                           join regdet in dbContext.registerdetails on reg.UserName equals regdet.UserName
                                           where userinfo.UserName == Session["MatriUser"].ToString()
                                           select new
                                           {
                                               reg.height, reg.region, reg.employed, userinfo.Location, reg.profession, reg.about,
                                               reg.bodytype, reg.cases,reg.complexion, reg.diet, reg.drink, reg.smoke,
                                               reg.education, reg.employment, reg.income,

                                           }).FirstOrDefault();


                      if(queryuserinfo!=null)
                      {
                          Height.Text = queryuserinfo.height;
                          Religion.Text = queryuserinfo.region.Remove(1).ToUpper()+queryuserinfo.region.Substring(1);
                          Working.Text = queryuserinfo.employed.Remove(1).ToUpper() + queryuserinfo.employed.Substring(1);
                          Living_In.Text = queryuserinfo.Location.Remove(1).ToUpper() + queryuserinfo.Location.Substring(1);
                          Profession.Text = queryuserinfo.profession.Remove(1).ToUpper() + queryuserinfo.profession.Substring(1);

                          BodyType.Text = queryuserinfo.bodytype.Remove(1).ToUpper() + queryuserinfo.bodytype.Substring(1);
                          SpecialCase.Text = queryuserinfo.cases.Remove(1).ToUpper() + queryuserinfo.cases.Substring(1);
                          Complexion.Text = queryuserinfo.complexion.Remove(1).ToUpper() + queryuserinfo.complexion.Substring(1);
                          Diet.Text = queryuserinfo.diet.Remove(1).ToUpper() + queryuserinfo.diet.Substring(1);
                          Drink.Text = queryuserinfo.drink.Remove(1).ToUpper() + queryuserinfo.drink.Substring(1);
                          Smoke.Text = queryuserinfo.smoke.Remove(1).ToUpper() + queryuserinfo.smoke.Substring(1);

                          HighestQualification.Text = queryuserinfo.education.Remove(1).ToUpper() + queryuserinfo.education.Substring(1);
                          Employed.Text = queryuserinfo.employment.Remove(1).ToUpper() + queryuserinfo.employment.Substring(1);
                          EmployedIn.Text = queryuserinfo.employed.Remove(1).ToUpper() + queryuserinfo.employed.Substring(1);
                          AnnualIncome.Text = queryuserinfo.income.Remove(1).ToUpper() + queryuserinfo.income.Substring(1);

                          about.Text = queryuserinfo.about.Remove(1).ToUpper() + queryuserinfo.about.Substring(1);
                      }

                       else
                      {
                          ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('Can't Retrieve Some Details. Please Try Again.')</script>");
                      }
 
Share this answer
 
Comments
Deepak Kanswal Sharma 9-Jan-16 0:32am    
But the problem now is if there is any null value in any database column. The values don't populate after populating that column. Can somebody please help me with this?
Deepak Kanswal Sharma 9-Jan-16 0:46am    
It's a LINQ to Dataset DBNULL problem / null reference exception and throws an 'Object reference not set to an instance of an object' Error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900