Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable button based on sql output parameter

Status
Not open for further replies.

neilmcdonald

Technical User
Aug 16, 2002
53
Hi,

I have a sqldatasource using a stored procedure with an output parameter. What I would like to do is enable/disable a button based on the value of that parameter.

I think my problem is to do with timing - I'm not sure at which point I should test the value of the parameter. I'm also not clear on whether the sqldatsource actually does it's select command when there's no control bound to it.

I'd appreciate any advice you can give me.

Thanks,

Neil
 
datasourcecontrols (while evil :) ) are ment to be used in conjunctions with databound controls like grid/detail/form/repeater controls.

if you want to use the sqldatasource to set a property of a non-databound control you will need to manuall execute the select command and parse the value.

for something such as this. I would just create a bool function to get the value in code.
Code:
protected override void OnLoad(EventArgs e)
{
   if (!Page.IsPostBack)
   {
      int id = getIdFromSomewhere();
      MyButton.Enabled = ShouldEnableControlBasedOn(id);
   }
}

private bool ShouldEnableControlBasedOn(int id)
{
   using(IDbConnection cnn = new SqlConnection())
   {
      IDbCommand cmd = cnn.CreateCommand();
      cmd.Text = "select 1 from table where id=@id";
      cmd.Parameters.Add("@id").Value = 1;
      return (bool)cmd.ExecuteScalar();
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top