Click here to Skip to main content
15,896,421 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True");
          
             SqlCommand cmd = new SqlCommand("select * from JUNIOR_STUDENT where @UID='+ @ " + txtUid.Text.Trim() + "'",con);
             cmd.CommandType = CommandType.Text;
             cmd.Parameters.AddWithValue("@UId", txtUid.Text);
             cmd.Parameters.AddWithValue("@NAME", txtName.Text);
             cmd.Parameters.AddWithValue("@ROLL_NO", txtRoll.Text);
             cmd.Parameters.AddWithValue("@STREAM", txtStream.Text);
             cmd.Parameters.AddWithValue("@GENDER", txtGender.Text);
             cmd.Parameters.AddWithValue("@YEAR_ID", cbxYearname.Text);
             cmd.Parameters.AddWithValue("@REMARKS", txtRemarks.Text);
             cmd.Parameters.AddWithValue("@ADMISSION_DATE", mtxtAdmsndate.Text);
             cmd.Parameters.AddWithValue("@ACADEMIC_YEAR", txtAcademicYear.Text);
             cmd.Parameters.AddWithValue("@ADMISSION_TYPE", txtAdmissiontype.Text);
             cmd.Parameters.AddWithValue("@CATEGORY_ID", cbxCategory.Text);
             cmd.Parameters.AddWithValue("@SUB_CATEGORY", txtName.Text);
             cmd.Connection = con;
             con.Open();
             
             SqlDataReader reader = cmd.ExecuteReader();
              while (reader.Read())
              {
              txtName.Text=reader["NAME"].ToString();
              txtRoll.Text = reader["ROLL_NO"].ToString();
              txtStream.Text = reader["STREAM"].ToString();
              txtGender.Text = reader["GENDER"].ToString();
              cbxYearname.Text = reader["YEAR_ID"].ToString();
              txtRemarks.Text = reader["REMARKS"].ToString();
              mtxtAdmsndate.Text = reader["ADMISSION_DATE"].ToString();
              txtAcademicYear.Text = reader["ACADEMIC_YEAR"].ToString();
              txtAdmissiontype.Text = reader["ADMISSION_TYPE"].ToString();
              cbxCategory.Text = reader["CATEGORY_ID"].ToString();
              txtSubcategory.Text = reader["SUB_CATEGORY"].ToString();
              
              
              reader.Close();
              con.Close();
              MessageBox.Show("Student : " + txtName.Text + " Transfer of Subject Successfull ", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
                  }
it show nothing wat can i do ??
Posted
Updated 5-May-14 10:19am
v3
Comments
José Amílcar Casimiro 5-May-14 16:24pm    
Sorry for the stupid question, but did you check that you had data in Db, right?
Member 10794814 6-May-14 0:31am    
@ Jose Amilcar Ferreira Cassimiro
yes it is... it has some value
Maciej Los 5-May-14 16:36pm    
What you mean by "show nothing"?
Member 10794814 6-May-14 0:31am    
its show nothing when i click on search button ,the value is not display in respective textbox.
any suggestion ???
Ishpreet Kaur 6-May-14 2:19am    
Sql query is not in proper format. That's why it is not showing result.

You have written:

select * from JUNIOR_STUDENT where @UID='+ @ " + txtUid.Text.Trim()

and this is wrong after Where clause.. You have written : "where @uid"

You should write:
where uid=....

not write @ before uid

1 solution

You did not provide enough information about your issue.

Does the reader contains anything? No! Have a look at your query:
C#
"select * from JUNIOR_STUDENT where @UID='+ @ " + txtUid.Text.Trim() + "'"

Your query should be:
SQL
select [Name], RollNo, Stream, Gender, Year_ID, Remarks, Admission_Date, Academic_Year, Admission_tye, Category_ID, Sub_Category
from JUNIOR_STUDENT
where UID=@UID


Finally, you are using only ine parameter @UID, so why do you set [Name], Rollno, etc?

BTW: Name is a reserved word, use it with brackets!

Corrected code:
C#
SqlCommand cmd = new SqlCommand("select * from JUNIOR_STUDENT where UDI=@UID",con);
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue("@UId", txtUid.Text);
             con.Open();
             SqlDataReader reader = cmd.ExecuteReader();


Here is a basic idea: HOW TO: Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET[^]. It's true, it's ASP.NET, but the logic is the same!
 
Share this answer
 
v2
Comments
Member 10794814 13-May-14 1:51am    
{SqlDataReader reader = cmd.ExecuteReader();}
Could not find stored procedure 'select * from JUNIOR_STUDENT where UID=@123'' new
error on this line !!!!!!
Maciej Los 13-May-14 2:05am    
Compare your both commands: your and mine. Do you see the difference?
Member 10794814 13-May-14 2:10am    
yes..
Member 10794814 13-May-14 2:11am    
but it show me another error
stored procedure not find..
any solution for this ??
Maciej Los 13-May-14 12:30pm    
Try to change: cmd.CommandType = CommandType.StoredProcedure; to cmd.CommandType = CommandType.Text;

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