Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Frnds,

I have tried the following code for Gridview values based on textbox. the following code is shown using search button. please help me..

C#
SqlConnection conn;
       conn = new SqlConnection();
       conn.ConnectionString = @"Data Source=Param-PC;Initial Catalog=HMS;Persist Security Info=True;User ID=sa;Password=123";
       conn.Open();
       string query;
       SqlCommand SqlCommand;
       SqlDataReader reader;
       SqlDataAdapter adapter = new SqlDataAdapter();
       query = "SELECT Reg_No,convert(nvarchar(10),Reg_Date,103) as Reg_Date, Patient_Name, Contact_No, Department_Name, convert(nvarchar(10),Date,103) as Date, Advance_Amt FROM In_Patients where Patient_Name='" + TextBox1.Text + "'";

       SqlCommand = new SqlCommand(query, conn);
       adapter.SelectCommand = new SqlCommand(query, conn);
       reader = SqlCommand.ExecuteReader();
       GridView1.DataSource = reader;
       GridView1.DataBind();
       reader.Close();
Posted
Updated 19-Feb-14 22:09pm
v3
Comments
King Fisher 20-Feb-14 4:04am    
getting any error?
The14thNoah 20-Feb-14 4:07am    
Dont use sqlReader,instead use Datatable to bind your data in your GridView
Member 10611310 20-Feb-14 4:22am    
No error.... values only not come..

Try this:

C#
SqlCommand = new SqlCommand(query, conn);
SqlDataAdapter da=new sqldataadapter(SqlCommand );
Dataset ds=new Dataset();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
reader.Close();
 
Share this answer
 
Comments
Member 10611310 20-Feb-14 4:27am    
and then Gridview is not come at page load in asp.net....
King Fisher 20-Feb-14 4:32am    
If you want at page load just load the Grid "SELECT Reg_No,convert(nvarchar(10),Reg_Date,103) as Reg_Date, Patient_Name, Contact_No, Department_Name, convert(nvarchar(10),Date,103) as Date, Advance_Amt FROM In_Patients" by this Query,
King Fisher 20-Feb-14 4:34am    
Accept it,if the above Solution is Worked.
Member 10611310 20-Feb-14 5:06am    
Not workd.. it comes error. datasourceid and datasource both declared in same gridview error come.. in coding gridview1.datasource=ds; remove answer not come..
King Fisher 20-Feb-14 5:09am    
show your Coding
Use dataset rather then datareader
 
Share this answer
 
if you want bind data to GridView By using SqlDataReader

SqlDataReader will returns the Row of data

So you want add row by row to table(create one new table)
then finally bind the Table data into your GridView.

or else u can use

SqlDataAdapter da=new sqldataadapter(query, conn);
Dataset ds=new Dataset();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
 
Share this answer
 

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