Button Column Select.


R

rcoco

Hi all,
I have a datagrid with Button column select in form of hyperlink. On
the same page, I have another datagrid that insert data and one
column(Name) has Names of a user that is inserting data(using windows
authentication).
Now this is how it's supposed to work: When any user want to see data
about a particular user it's a matter of selecting his name and the
data Writen by the selected user should be the only ones to be shown.
I got some examples on Google but they all seem not to be working.
When a user is selected the page remains the same data is not
selected. How would can I solve this problem?
My code looks like this:
HTML part:
<Columns>
<asp:ButtonColumn HeaderText="Operations"
DataTextField="TeamOperation" ButtonType="LinkButton"></
asp:ButtonColumn>
</Columns>
and code behind:
private void dgoperation_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="select * from dbo.DashBoard where Name =
@Billing";
myCommand.Parameters.Add(new SqlParameter("@Billing",SqlDbType.VarChar,
50));
myCommand.Parameters["@Billing"].Value= dgbilling;
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds);
dgis.DataSource=ds;
dgis.EditItemIndex = -1;
dgis.DataBind();
}

dgis: this is the datagrid that where user inserts data.
dgbilling: this is the datagrid with the name list.
Thanks.
 
Ad

Advertisements

R

rcoco

I have corrected my statement and this time I'm getting this error:
"Object reference not set to an instance of an object."
And the error source:
Line 219: myCommand.CommandText="select * from dbo.DashBoard where
Name Like @Billing";
Line 220: myCommand.Parameters.Add(new
SqlParameter("@Billing",SqlDbType.VarChar,50));
Line 221: myCommand.Parameters["@Billing"].Value= bc.Text;
Line 222: SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
Line 223: DataSet ds = new DataSet();
This is my code:
<Columns>
<asp:ButtonColumn HeaderText="Operations"
DataTextField="TeamOperation" ButtonType="LinkButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
My code behind:
private void dgoperation_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.LinkButton bc=new
System.Web.UI.WebControls.LinkButton();
bc=(System.Web.UI.WebControls.LinkButton)e.Item.Cells[0].FindControl("TeamBilling");
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="select * from dbo.DashBoard where Name Like
@Billing";
myCommand.Parameters.Add(new SqlParameter("@Billing",SqlDbType.VarChar,
50));
myCommand.Parameters["@Billing"].Value= bc.Text;
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds);
dgis.DataSource=ds;
dgis.EditItemIndex = -1;
dgis.DataBind();
}
How can I solve this?
Thanks
 
A

Alexey Smirnov

myCommand.Connection=con;

What is 'con'? Did you created a SqlConnection reference already?

For example,

SqlConnection con = new SqlConnection("my_connection_string");
 
R

rcoco

Hi Alexey,
con is:
SqlConnection con = new SqlConnection("user
id=utldbuser;"+"password=utldbuser;"+"server=utlhq202;"+"database=IS_dashboard;");
Thanks
 
A

Alexey Smirnov

Hi Alexey,
con is:
SqlConnection con = new SqlConnection("user
id=utldbuser;"+"password=utldbuser;"+"server=utlhq202;"+"database=IS_dashbo­ard;");
Thanks

Well, but where do you create it?

The code above looks correct and I can only guess that possibly your
connection object is not a global available reference.

Is it a code-behind class? If yes, did you compiled it?
 
R

rcoco

Well, but where do you create it?

The code above looks correct and I can only guess that possibly your
connection object is not a global available reference.

Is it a code-behind class? If yes, did you compiled it?

Yes it's a code behind.
I think the problem was
System.Web.UI.WebControls.LinkButton bc=new
System.Web.UI.WebControls.LinkButton();
bc=(System.Web.UI.WebControls.LinkButton)e.Item.Cells[0].FindControl("TeamB­
illing");
So I have now tryed Writing the code like this.
System.Web.UI.WebControls.ButtonColumn bc=new
System.Web.UI.WebControls.ButtonColumn();
bc=(System.Web.UI.WebControls.ButtonColumn)e.Item.Cells[0].FindControl("Select") ;
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="select * from dbo.DashBoard where Name =
@operation";
myCommand.Parameters.Add(new
SqlParameter("@operation",SqlDbType.VarChar,50));
myCommand.Parameters["@operation"].Value= bc.DataTextField;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgis.EditItemIndex=-1;
Fill();
Bind();
But when compiling I get this error
C:\Inetpub\wwwroot\Dash_Board\DashBoard.aspx.cs(210): Cannot convert
type 'System.Web.UI.Control' to
'System.Web.UI.WebControls.ButtonColumn'
I'm not sure if I'm on the right track! What do you think?
Thank you very much.
 
Ad

Advertisements

A

Alexey Smirnov

Well, but where do you create it?
The code above looks correct and I can only guess that possibly your
connection object is not a global available reference.
Is it a code-behind class? If yes, did you compiled it?

Yes it's a code behind.
I think the problem was
System.Web.UI.WebControls.LinkButton bc=new
System.Web.UI.WebControls.LinkButton();
bc=(System.Web.UI.WebControls.LinkButton)e.Item.Cells[0].FindControl("TeamB­­
illing");
So I have now tryed Writing the code like this.
System.Web.UI.WebControls.ButtonColumn bc=new
System.Web.UI.WebControls.ButtonColumn();
bc=(System.Web.UI.WebControls.ButtonColumn)e.Item.Cells[0].FindControl("Sel­ect") ;
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="select * from dbo.DashBoard where Name =
@operation";
myCommand.Parameters.Add(new
SqlParameter("@operation",SqlDbType.VarChar,50));
myCommand.Parameters["@operation"].Value= bc.DataTextField;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgis.EditItemIndex=-1;
Fill();
Bind();
But when compiling I get this error
C:\Inetpub\wwwroot\Dash_Board\DashBoard.aspx.cs(210): Cannot convert
type 'System.Web.UI.Control' to
'System.Web.UI.WebControls.ButtonColumn'
I'm not sure if I'm on the right track! What do you think?
Thank you very much.

First of all, remember, the "code behind" classes must be compiled
into an assembly, which is placed by VS.NET in the \bin directory.
When you get an error during compilation, you cannot run the actual
code, because your classes simply did not compiled and a DLL in the
\bin directory was not updated.

Regarding your LinkButton.

I'm not very well understood why you used this

<asp:ButtonColumn DataTextField="TeamOperation" ...

Maybe you can explain, what you did supposed to get there

I suppose, you wanted to have this

<asp:ButtonColumn CommandName="TeamOperation"....

which can be simply accessed as

e.CommandName

in your dgoperation_ItemCommand() function

e.g.

myCommand.Parameters["@operation"].Value= e.CommandName;

without any extra code.

However, in this case I don't see the sense of the

"select * from dbo.DashBoard where Name = @operation";
 
R

rcoco

Yes it's a code behind.
I think the problem was
System.Web.UI.WebControls.LinkButton bc=new
System.Web.UI.WebControls.LinkButton();
bc=(System.Web.UI.WebControls.LinkButton)e.Item.Cells[0].FindControl("TeamB­­­
illing");
So I have now tryed Writing the code like this.
System.Web.UI.WebControls.ButtonColumn bc=new
System.Web.UI.WebControls.ButtonColumn();
bc=(System.Web.UI.WebControls.ButtonColumn)e.Item.Cells[0].FindControl("Sel­­ect") ;
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="select * from dbo.DashBoard where Name =
@operation";
myCommand.Parameters.Add(new
SqlParameter("@operation",SqlDbType.VarChar,50));
myCommand.Parameters["@operation"].Value= bc.DataTextField;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgis.EditItemIndex=-1;
Fill();
Bind();
But when compiling I get this error
C:\Inetpub\wwwroot\Dash_Board\DashBoard.aspx.cs(210): Cannot convert
type 'System.Web.UI.Control' to
'System.Web.UI.WebControls.ButtonColumn'
I'm not sure if I'm on the right track! What do you think?
Thank you very much.

First of all, remember, the "code behind" classes must be compiled
into an assembly, which is placed by VS.NET in the \bin directory.
When you get an error during compilation, you cannot run the actual
code, because your classes simply did not compiled and a DLL in the
\bin directory was not updated.

Regarding your LinkButton.

I'm not very well understood why you used this

<asp:ButtonColumn DataTextField="TeamOperation" ...

Maybe you can explain, what you did supposed to get there

I suppose, you wanted to have this

<asp:ButtonColumn CommandName="TeamOperation"....

which can be simply accessed as

e.CommandName

in your dgoperation_ItemCommand() function

e.g.

myCommand.Parameters["@operation"].Value= e.CommandName;

without any extra code.

However, in this case I don't see the sense of the

"select * from dbo.DashBoard where Name = @operation";- Hide quoted text -

- Show quoted text -
Thanks
TeamOperation is a columns that has a list of users of the website. So
by
<asp:ButtonColumn DataTextField="TeamOperation" ... Im diplaying the
names in datagrid "dgoperation".
Thanks
 
A

Alexey Smirnov

Thanks
TeamOperation is a columns that has a list of users of the website. So
by
<asp:ButtonColumn DataTextField="TeamOperation" ... Im diplaying the
names in datagrid "dgoperation".
Thanks- Hide quoted text -

Ok, I think, I got it

Then try

myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;

assuming that "Operations" is the first column of the grid
 
R

rcoco

Thanks
TeamOperation is a columns that has a list of users of the website. So
by
<asp:ButtonColumn DataTextField="TeamOperation" ... Im diplaying the
names in datagrid "dgoperation".
Thanks- Hide quoted text -

Ok, I think, I got it

Then try

myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;

assuming that "Operations" is the first column of the grid

Still does not select any data. All it does its like the page is just
being refreshed.
Thanks
 
A

Alexey Smirnov

Ok, I think, I got it
myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;
assuming that "Operations" is the first column of the grid

Still does not select any data. All it does its like the page is just
being refreshed.
Thanks- Hide quoted text -

- Show quoted text -

Well, at least there is no compilation error :)

Try to see what you pass to the database: add a debug string, for
example

myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;
Response.Write "select * from dbo.DashBoard where Name = '" +
e.Item.Cells[0].Text + "'";

This would return a sql that you can execute against your database to
see if it is correct or not.
 
Ad

Advertisements

R

rcoco

Thanks
TeamOperation is a columns that has a list of users of the website. So
by
<asp:ButtonColumn DataTextField="TeamOperation" ... Im diplaying the
names in datagrid "dgoperation".
Thanks- Hide quoted text -
Ok, I think, I got it
Then try
myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;
assuming that "Operations" is the first column of the grid
Still does not select any data. All it does its like the page is just
being refreshed.
Thanks- Hide quoted text -
- Show quoted text -

Well, at least there is no compilation error :)

Try to see what you pass to the database: add a debug string, for
example

myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;
Response.Write "select * from dbo.DashBoard where Name = '" +
e.Item.Cells[0].Text + "'";

This would return a sql that you can execute against your database to
see if it is correct or not.- Hide quoted text -

- Show quoted text -

I get select*from dbo.DashBoard where Name=
That's allit returns.
Thanks
 
R

rcoco

Thanks
TeamOperation is a columns that has a list of users of the website. So
by
<asp:ButtonColumn DataTextField="TeamOperation" ... Im diplaying the
names in datagrid "dgoperation".
Thanks- Hide quoted text -
Ok, I think, I got it
Then try
myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;
assuming that "Operations" is the first column of the grid
Still does not select any data. All it does its like the page is just
being refreshed.
Thanks- Hide quoted text -
- Show quoted text -

Well, at least there is no compilation error :)

Try to see what you pass to the database: add a debug string, for
example

myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;
Response.Write "select * from dbo.DashBoard where Name = '" +
e.Item.Cells[0].Text + "'";

This would return a sql that you can execute against your database to
see if it is correct or not.- Hide quoted text -

- Show quoted text -

It return select* from dbo.DashBoard where Name=
That is all it returns
 
A

Alexey Smirnov

myCommand.Parameters["@Billing"].Value= e.Item.Cells[0].Text;
Response.Write "select * from dbo.DashBoard where Name = '" +
e.Item.Cells[0].Text + "'";

My mistake, sorry!

e.Item.Cells[0].Text will work for "normal" cells only, for the
ButtonColumn retrieving the text can be done with the reference to a
control and cast it to the LinkButton class

So, you @Billing value has to be as:

myCommand.Parameters["@Billing"].Value = ((LinkButton)
e.Item.Cells[0].Controls[0]).Text;

To be sure

Response.Write "select * from dbo.DashBoard where Name = '" +
((LinkButton) e.Item.Cells[0].Controls[0]).Text + "'";
 
A

Alexey Smirnov

Alexey Smirnov said:
Response.Write "select * from dbo.DashBoard where Name = '" +
((LinkButton) e.Item.Cells[0].Controls[0]).Text + "'";

Response.Write(); should be with the parentheses
 
R

rcoco

Response.Write "select * from dbo.DashBoard where Name = '" +
((LinkButton) e.Item.Cells[0].Controls[0]).Text + "'";

Response.Write(); should be with the parentheses
Hi Alexey,
When I try:
Response.Write("select*from dbo.DashBoard where Name="+
((LinkButton)e.Item.Cells[0].Controls[0]).Text+"");
It returns select*from dbo.DashBoard where Name=Claude
Claude is the selected Name which is ok.
But when I now try
myCommand.Parameters["@operation"].Value=
((LinkButton)e.Item.Cells[0].Controls[0]).Text;
Data is not selected why is it so?
But I do not get any error.
Thanks.
 
Ad

Advertisements

A

Alexey Smirnov

Response.Write "select * from dbo.DashBoard where Name = '" +
((LinkButton) e.Item.Cells[0].Controls[0]).Text + "'";
Response.Write(); should be with the parentheses

Hi Alexey,
When I try:
Response.Write("select*from dbo.DashBoard where Name="+
((LinkButton)e.Item.Cells[0].Controls[0]).Text+"");
It returns select*from dbo.DashBoard where Name=Claude
Claude is the selected Name which is ok.
But when I now try
myCommand.Parameters["@operation"].Value=
((LinkButton)e.Item.Cells[0].Controls[0]).Text;
Data is not selected why is it so?
But I do not get any error.
Thanks.

Go to your database and execute this sql statement

select * from dbo.DashBoard where Name='Claude'

What result do you receive?
 
R

rcoco

Thanks Alexey,
I Found out the solution I had To write:
myCommand.CommandText="select * from dbo.DashBoard where Name Like
@billing";
myCommand.Parameters.Add(new SqlParameter("@billing",SqlDbType.VarChar,
50));
myCommand.Parameters["@billing"].Value=
((LinkButton)e.Item.Cells[0].Controls[0]).Text;
It worked properly with out any problem.
Thank you very very much You are such a nice person you helped me a
lot.
Have a nice day.
 
Ad

Advertisements

A

Alexey Smirnov

Thanks Alexey,
I Found out the solution I had To write:
myCommand.CommandText="select * from dbo.DashBoard where Name Like
@billing";
myCommand.Parameters.Add(new SqlParameter("@billing",SqlDbType.VarChar,
50));
myCommand.Parameters["@billing"].Value=
((LinkButton)e.Item.Cells[0].Controls[0]).Text;
It worked properly with out any problem.
Thank you very very much You are such a nice person you helped me a
lot.
Have a nice day.

Hej, great! You see, it was not that difficult to get it working :)
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Using HyperlinkColumn! 4
Prolem with parameter! 3
DropDownList 1
Inserting a new row. 3
adding a row on a datagrid 3
Datagrid fails to show data! 6
dropdownlist 4
System.Data.DataRow error 1

Top