Column names in C#

Posted on 2011-10-12
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 43

    Accepted Solution

    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

    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 Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now