[Last Call] Learn how to a build a cloud-first strategyRegister Now


Column names in C#

Posted on 2011-10-12
Medium Priority
Last Modified: 2012-05-12
Hello, I made an Excel spreadsheet a datatable and I am trying to get information from the columns via using a column name in C#.  The problem I am having is my C# error says that the column does not belong in the table.  So what kind of format issue am I having?

for (int j = 0; j < dt.Rows.Count; j++)
                    //GroupName1 GroupName2 GroupName3 Snd_Nbr Rcv_Nbr Snd_Size Rcv_Size PartnerTypeName Id_Recipient Id_Regroup SelRecip
                    string sql = "";
                    sql =
                        "insert mpExchangeActivityByUser " +
                        "select " + MP.Database.Utils.FxQs(dt.Rows[j]["Snd_Nbr"].ToString(), "'") + ", " +
                        "       " + MP.Database.Utils.FxQs(dt.Rows[j]["Snd_Size"].ToString(), "'") + ", " +
                        "       " + MP.Database.Utils.FxQs(dt.Rows[j]["SelRecip"].ToString().Replace("\"", ""), "'") + ", " +
                        "       " + MP.Database.Utils.FxQs(senddate.ToShortDateString(), "'") + " ";

                    sql = "";

Open in new window

Question by:VBBRett
LVL 45

Accepted Solution

AndyAinscow earned 1000 total points
ID: 36960992
Have you checked the rows to find the problem field ?

String s = dt.Rows[0]["Snd_Nbr"].ToString();
s = dt.Rows[0]["Snd_Size"].ToString();
s = dt.Rows[0]["SelRecip"].ToString();

I guess one (or more) of those will fail when you single step.

IF all of those work then check the sql statement within the loop - maybe something is formed incorrectly there.
LVL 22

Assisted Solution

Mrunal earned 1000 total points
ID: 36961199
you can add if condition for this as:

if(dt != null)
if(dt.Rows.Count > 0)
if(Convert.ToString("dt.Rows[0]["Snd_Nbr"]").Trim() ! = '' && Convert.ToString("dt.Rows[0]["Snd_Size"]").Trim() ! = ''  && Convert.ToString("dt.Rows[0]["SelRecip"]").Trim() ! = '' )
//your code goes here

while debugging, check which column you are missing.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question