Retrieving values from a dataset

Hi, I am trying use the function below to get values from the dataset.
The Stored Procedure returns records, it goes into the if loop below..but when it comes to this line:
Criteria.Text = ds.Tables["dbo.ExceptionRuleDetail"].Rows[0]["rl_criteria1"].ToString();
it is throwing an exception "Object reference not set to an instance of an object".
Table :ExceptionRuleDetail" and column "rl_criteria1" exists in the database.

Can you please tell me why this error occurs?
protected void ddsavedsearch_SelectedIndexChanged(object sender, EventArgs e)
        SqlDataReader rdr = null;
        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = (ConfigurationManager.AppSettings["Gulfcoast"]);
        SqlCommand cmd = new SqlCommand("SP_GetSavedInvestigation", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@rlname", SqlDbType.NVarChar).Value = ddsavedsearch.SelectedValue.ToString();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        string table_name = string.Empty;

      table_name = "ExceptionRuleDetail";
       if((ds!=null) && (ds.Tables.Count>0) && (ds.Tables[0].Rows.Count > 0))
            Criteria.Text = ds.Tables["dbo.ExceptionRuleDetail"].Rows[0]["rl_criteria1"].ToString();
            Category1.SelectedItem.Value = ds.Tables["ExceptionRuleDetail"].Rows[0]["rl_category1"].ToString();
            Criteria1.Text = ds.Tables["ExceptionRuleDetail"].Rows[0]["rl_criteria2"].ToString();
            Category2.Text = ds.Tables["ExceptionRuleDetail"].Rows[0]["rl_category2"].ToString();

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
i dont see any loop
try referrring the table by index instead of name


also ou can check the table name if you add this to watch window ds.Tables[0] after putting a breakpoint...
Tom BeckCommented:
Should this line:

Criteria.Text = ds.Tables["dbo.ExceptionRuleDetail"].Rows[0]["rl_criteria1"].ToString();

Criteria.Text = ds.Tables["dbo.ExceptionRuleDetail"].Rows[0].Item["rl_criteria1"].ToString();
Did you run sp_createstats stored procedure against this table? This could create the statistics.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

anarki_jimbelSenior DeveloperCommented:
Hard to say what's going one.

I suggestSome code improvement:

In your "if" statement you refer the table by index (ds.Tables[0].Rows.Count > 0) but then you refer it by name. Are you sure it's the same table?

If it same - it has rows. Try to access fields by index and check column names, I have suspicion it is wrong.
And yes, use DataRow.Item property (see tommyBoy's comment)
WebRulesAuthor Commented:
I meant the if condition...not loop. sorry.

It worked by referring the table with an Tables[0].

Thank you.
WebRulesAuthor Commented:
@evaryone else..thank you for your prompt replies. thanks:)
WebRulesAuthor Commented:
I also have another question.
Does hyperlink have an onlick property where we can call a function on that click?
HainKurtSr. System AnalystCommented:

<a href="#" onClick="myFunction()">click</a>


<a href="javascript:myFunction()">click</a>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WebRulesAuthor Commented:
Ya, I did this in the same way as below:

<asp:HyperLink ID="hyperlink" runat="server" NavigateUrl='<%# Eval("ri_id","../Investigations/InvestigationsPage.aspx?id={0}")%>' onclick="BindSearch()"">
    <%#Eval("ri_store_name")%> from <%#Eval("ri_fromDate") %>-<%#Eval("ri_toDate") %>

But when i debugged and checked, the function is not being called.
Is there any error ?
HainKurtSr. System AnalystCommented:
you should use thisa code:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.