query to return n item using a unique id

Posted by Jopito under ASP.NET on 12/6/2013 | Points: 10 | Views : 1017 | Status : [Member] | Replies : 8
is their a way i can make my method to return the Student name on selecting of studentId?have one which returns null but cannot fix whats wrong with it.
Thanks

Mark as answer if satisfied


Responses

Posted by: vishalneeraj-24503 on: 12/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
you can use cmd.ExecuteScalar() to return student_name
as

object student_name = cmd.ExecuteScalar();
where cmd is your SqlCommand object.

like

private string get_name(int id)
{
SqlConnection con = new SqlConnection("write your connection name");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select student_name from student_master where student_id = '" & id & "'";
cmd.Connection = con;
object name = cmd.ExecuteScalar();
string student_name = Convert.ToString(name);

return student_name;
}



Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://forums.asp.net/t/1908673.aspx
http://mentormate.com/blog/improving-linq-to-entities-queries/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/6/2013 [Member] Starter | Points: 25

Up
0
Down
i was doing this one in c# but the method has a n issue
This is wat am using
public Students GetStudentName(string StudentName)
{

in mi code behind i have this code below

Student newStudent = db.Students.SingleOrDefault(studentname =>studentname.StudentName == studentName);
return newStudent;
This one cannot return any data

StudentNameRadtextbox.Text =Studentsdata.GetStudentName(studentname).StudentId.ToString();



}


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
string studentQuery =
(from student in students
where student.id == 90
select student.Name).FirstOrDefualt();


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Use FirstOrDefualt instead of SingleOrDefault.

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/6/2013 [Member] Starter | Points: 25

Up
0
Down
hi,when i pass a paremeter in the method from the database it picks e. when i enter the studentname it shows the value but if i dont provide it cannot return any value ,how i can i bind it to the item selected from the comobobox as it returns nothing wen i do that,
This is whereby when i add an item from databse

StudentNameRadtextbox.Text =Studentsdata.GetStudentName(studentname) .StudentId.ToString();
when i replace the student name with a name from database it shows up,

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/6/2013 [Member] Starter | Points: 25

Up
0
Down
Have used first or default and still getting same results

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response