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?
Please.
protected void ddsavedsearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataReader rdr = null;
        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = (ConfigurationManager.AppSettings["Gulfcoast"]);
        conn.Open();
        SqlCommand cmd = new SqlCommand("SP_GetSavedInvestigation", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@rlname", SqlDbType.NVarChar).Value = ddsavedsearch.SelectedValue.ToString();
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        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

WebRulesAsked:
Who is Participating?
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

ds.Tables[0].Rows[0]["rl_criteria1"].ToString();

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

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

Criteria.Text = ds.Tables["dbo.ExceptionRuleDetail"].Rows[0].Item["rl_criteria1"].ToString();
0
sventhanCommented:
Did you run sp_createstats stored procedure against this table? This could create the statistics.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

anarki_jimbelCommented:
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)
0
WebRulesAuthor Commented:
@HainKurt:
I meant the if condition...not loop. sorry.

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

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


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

or

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

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") %>
     </asp:HyperLink>

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

hyperlink.attributes.add("onclick","BindSearch()")
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.