如何使用Linq从数据库中获取值到文本框中
问题描述:
Hello All,
如何从数据库中获取值到我的aspx页面上的文本框中。到目前为止,我已经这样做了: -
Hello All ,
How can I get values from database into the textboxes on my aspx page. So far I've done this:-
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....
答
假设这有效,有几个问题。
1. queryuserinfo是一个匿名项列表。这是因为select新语句。我认为你的意图是选择一个信息类。 - 选择新信息...然后queryuserinfo将是一个类似于myinfo的信息对象列表。
示例:
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:
var queryuserinfo = (from User_Information in dbContext.User_Informations
where User_Information.UserName == Session["MatriUser"].ToString()
select new info
info需要是你定义的一个类。我只能假设已经完成了因为代码无法在你的行上编译
info needs to be a class you define. I could only assume that was already done because code would not compile on your line
IEnumerable<info>myinfo
</info>
因为它定义了信息类型。
无论如何,这里有一些参考资料供你学习:
https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
和Linqpad示例:
LINQPad示例 - 主页 [ ^ ]
见Linqpad:
https://www.linqpad.net
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
我得到了解决方案: -
I got the solution somehow:-
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>");
}