locked
using sqldatasource1.select() correctly RRS feed

  • Question

  • User199372478 posted

    I have a query that looks like this attached to a sqldatasource :

    SELECT [fldBaseline] FROM [BI_PATHS_Target_Baseline] WHERE fldBranch = @fldBranch and fldyear=@fldyear and fldcategory = @cat

    and i want to make a LABEL to read that fldbaseline value and i tried this :

    System.Data.DataView dv = (System.Data.DataView)Sql_LEAN_baselineupdate.Select();
                int eric = (int)dv.Table.Rows[0][0];
                Label_lean_baseline.Text = eric.ToString();


     

    btw the @fldbranch is pulling from a dropdownlist and so it the @year but the @cat is just defined always as 'LEAN'

    But im not sure what to put inside the select() part. i know you have to put datasourceselectarguments.empty if no parameters, but what would I do here since their are 3?

     

    Thanks to all the help!!

    -Eric Pacheco

    Friday, January 14, 2011 9:27 AM

Answers

  • User626880745 posted

    You can add those Parameters before you execute .Select(DataSourceSelectArguments.Empty)


    something like:

    Sql_LEAN_baselineupdate.SelectParameters.Add("fldBranch", string value here);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 14, 2011 10:08 AM

All replies

  • User626880745 posted

    You can add those Parameters before you execute .Select(DataSourceSelectArguments.Empty)


    something like:

    Sql_LEAN_baselineupdate.SelectParameters.Add("fldBranch", string value here);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 14, 2011 10:08 AM
  • User199372478 posted

    I tried this(which i know has a value of 100 in the database) :

                Sql_LEAN_baselineupdate.SelectParameters.Add("fldBranch", "1780");
                Sql_LEAN_baselineupdate.SelectParameters.Add("fldYear", "2011");
                Sql_LEAN_baselineupdate.SelectParameters.Add("fldCategory", "LEAN");
                System.Data.DataView dv = (System.Data.DataView)Sql_LEAN_baselineupdate.Select(DataSourceSelectArguments.Empty);
                int eric = (int)dv.Table.Rows[0][0];
                Label_lean_baseline.Text = eric.ToString();


     

    but i get this error

    int eric = (int)dv.Table.Rows[0][0] : Object reference not set to an instance of an object

    but i know there is a value of 100 there.

    Friday, January 14, 2011 10:55 AM
  • User626880745 posted

    make sure that the data object has data. you can check for null and dv.Count

    Friday, January 14, 2011 11:06 AM
  • User199372478 posted
    if (dv.Count > 0)
                {
                    Label_lean_baseline.Text = "dv count" + dv.Count;
                }
                else
                {
                    Label_lean_baseline.Text = "no data";
                }

    if (dv.Count > 0) : Object reference not set to an instance of an object.

     im getting that error when I try to check for data.

    i think it has something to do with the declare of dv...i dont think its working right.

    Friday, January 14, 2011 11:59 AM
  • User626880745 posted

    The declare looks alright to me. Is dv == null ?

    Alternatively, you could have used a SqlCommand instead (the ExecuteScalar method to return a single value), here's an example: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

    Friday, January 14, 2011 12:33 PM
  • User199372478 posted

    I figured it out... for some reason the script wont work under page_load...but when i put it on a button...worked perfect! thanks PeteNet!

     

    Friday, January 14, 2011 4:45 PM