Need help in gridview

Posted by Rickeybglr under ASP.NET on 9/14/2012 | Points: 10 | Views : 926 | Status : [Member] | Replies : 4
Hi All,

i have a store procedure with some parameters. but i dnt want to display all the columns in a grid view, so have to hide some columns. the othr thing is one column contain imagepath. so need to bind this with imageField of datagrid view. how can i do ths. i dnt wanna use sqlDataSource wizard;
here is my code:
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("USP_ExtendedSearch", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@searchUsers", txtContactsSearch.Text.Trim());
cmd.Parameters.AddWithValue("@Name", txtSearchNames.Text.Trim());
cmd.Parameters.AddWithValue("@City", txtSearchCities.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtEmail.Text.Trim());
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
SqlDataReader reader = cmd.ExecuteReader();
gv.DataSource = reader;
gv.DataBind();
conn.Close();


store proc:
LTER PROCEDURE [dbo].[USP_ExtendedSearch]
(
--@userName NVARCHAR(30),
@searchUsers NVARCHAR(30),
@Name NVARCHAR(30),
@City NVARCHAR(30),
@email NVARCHAR(30)
)
AS
BEGIN
SELECT a.userID,a.username AS UName,
a.firstname+' '+a.lastname AS Name,
a.useremail AS EmailID,
a.city AS City,
b.imagePath as ImagePath

FROM userRegistrationTB a
JOIN imageTB b
ON
a.userID=b.UserID

WHERE
--a.username!=@userName
--AND
a.username LIKE '%'+ISNULL(@searchUsers,'')+'%'
AND a.firstname LIKE '%'+ISNULL(@Name,'')+'%'
AND a.city LIKE '%'+ISNULL(@City,'')+'%'
AND a.useremail LIKE '%'+ISNULL(@email,'')+'%'

SET NOCOUNT ON;


END




Responses

Posted by: Chandudotnet on: 9/14/2012 [Member] Starter | Points: 25

Up
0
Down
//use datatable object insted of SqlDataReader reader = cmd.ExecuteReader();



like
Datatable dt=new Datatable();
da.Fill(dt);


//After that

GridView1(ID).Datasource=dt


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

Posted by: Rickeybglr on: 9/14/2012 [Member] Starter | Points: 25

Up
0
Down
i have change the code but getting index out of range error

SqlCommand cmd = new SqlCommand("USP_ExtendedSearch", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@searchUsers", txtContactsSearch.Text.Trim());
cmd.Parameters.AddWithValue("@Name", txtSearchNames.Text.Trim());
cmd.Parameters.AddWithValue("@City", txtSearchCities.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtEmail.Text.Trim());
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
conn.Close();
gv.Columns[0].Visible = false;
gv.Columns[1].Visible = false;
gv.Columns[2].Visible = false;

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

Posted by: Hariinakoti on: 9/14/2012 [Member] Starter | Points: 25

Up
0
Down
i think u miss the some column name or u r not entered that column value.in that time only we got that error.check it once ur code.

Thanks & Regards
Hari

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

Posted by: Rickeybglr on: 9/14/2012 [Member] Starter | Points: 25

Up
0
Down
do i need to create the column also?? ths is a a complete code wat i have written??

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

Login to post response