how to get data using one column through data reader into gridview

Posted by Kishore463 under ASP.NET on 7/24/2013 | Points: 10 | Views : 1149 | Status : [Member] | Replies : 3
is it posible to get data from database into data reader and data give to gridview using where statement in select query




Responses

Posted by: Jitendrasoft09 on: 7/24/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down

Yes, Nice question Kishore. SqlDataReader has Read() function to read data from database. It has also GetSchemaTable to get table structure to further change to display column wise or full table or with select query.

Make sure you are clicking below link to understanding more-
http://www.dotnetcurry.com/ShowArticle.aspx?ID=143




Jitendra Kumar
If my post helps you, plz mark as an answer.

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

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

Up
0
Down
Hi Kishore,

I think we can do by using WHERE condition in the SQL query itself... Look into the following code...
[code]
protected void Page_Load(object sender, EventArgs e)
{
string LastName = ""; //Pass on your string value
if (!IsPostBack)
{
BindGrid(LastName);
}
}

private DataTable GetRecords(string LastName)
{
// Fill in your database details; else use web.config method
string strConnection = "SERVER=XXXXX;Database=XXXXX;User Id=XXXX;Password=XXXXX";
SqlConnection conn = new SqlConnection(strConnection);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from DotNet.Employees where last_name = '" + LastName + "';";
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
DataSet objDs = new DataSet();
dAdapter.Fill(objDs);
return objDs.Tables[0];

}
private void BindGrid(string bndLastName)
{
DataTable dt = GetRecords(bndLastName);
if (dt.Rows.Count > 0)
{
grdSearch.DataSource = dt;
grdSearch.DataBind();
}
}[/code]
Note: Alternate method is to use GridView along with Search Button


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

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

Posted by: Satyapriyanayak on: 7/25/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Gridview_reader._Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Gridview_reader
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;


protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);

str = "select * from employee where EmpId like '" + TextBox1.Text + "%'";
com = new SqlCommand(str, con);
con.Open();
SqlDataReader reader;
reader = com.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
con.Close();
}
}
}


If this post helps you mark it as answer
Thanks

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

Login to post response