Hi I have a datatable that returns 3 columns, what I am trying to do is to return the second column of a row when a variable matches the first. It's probably really simple but I can't get it to work. Thanks in advance.

Hi

Can you provide the code that you are currently trying to get working and a bit more of an idea of what you mean by "second column of a row when a variable matches the first".

Yea the code is:

 var datatable = conn.SPExecuteDataTable(sql);

            List<string> reports = new List<string>();
            List<string> reportShort = new List<string>();

            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                reports.Add(datatable.Rows[i]["report_name"].ToString());
            }

            System.Text.StringBuilder sb = new StringBuilder();
            sb.AppendLine("<ul>");
            if (!Page.IsPostBack)
            {
                foreach (string item in reports)
                {

                        sb.AppendLine(string.Format("<li><a href = reports.aspx?ReportName=" + HttpUtility.UrlEncode(item) + "><span class='glyphicon glyphicon-file'></span>{0}</a></li>", item)); 
                }
            }

This is the code I have, and my datatable returns 3 columns, report_name, report_short, report_full. At the minute the code saves the report_name in a list and appends each as <a href> in a menu, however I need the menu to use the report_short values from the datatable but need the report_name to call another stored proc. Hope I explained it better this time. what i really need is if(item == value in datatable) return that rows second value.

Hi

Why not add the report_short value to the query string along with the name of the report. This is assuming that report_short is quite a short value as there are limitations to the amount of data you can put on a query string. To do this, I would modify your original code so that you build the menu within your loop of the DataTable:

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.AppendLine("<ul>");

            foreach (DataRow row in datatable.Rows)
            {
                sb.AppendLine(String.Format("<li><a href=Report.aspx?ReportName={0}&ReportShort={1}>{2}</a></li>" ,  HttpUtility.UrlEncode(row["report_name"].ToString()), HttpUtility.UrlEncode(row["report_short"].ToString()), row["report_name"].ToString()));
            }

            sb.AppendLine("</ul>");

Then when you read this data back using Request.QueryString you can read either the "report_name" for the other stored procedure or "report_short" for whatever purpose it is you require it. Note, that with this method you do not need to store the report names in a list.

HTH

Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.