How to fix runtime error while loading data from database “An object or column name is missing or empty.”How to fix “Referenced assembly does not have a strong name” error?How can I get column names from a table in SQL Server?An object or column name is missing or emptydon't see the new database and can't log inHow to change column datatype in SQL database without losing dataExporting Large Amounts of DataAn object or column name is missing or empty (0x80131904)An object or column name is missing or empty GEOMETRYFill Dataset AsyncRetrieving the data from the database based on the parameter

Is there an application which does HTTP PUT?

Creating Stored Procedure in local db that references tables in linked server

How to avoid making self and former employee look bad when reporting on fixing former employee's work?

What will Doctor Strange protect now?

Can radiation block all wireless communications?

Why is it wrong to *implement* myself a known, published, widely believed to be secure crypto algorithm?

What's the difference between "ricochet" and "bounce"?

Light Switch Neutrals: Bundle all together?

99 coins into the sacks

Employee is self-centered and affects the team negatively

Trying to understand a summation

Linear Independence for Vectors of Cosine Values

Why is the episode called "The Last of the Starks"?

When an electron around an atom drops to a lower state, is 100% of the energy converted to a photon?

Why did Missandei say this?

How do I politely tell my players to shut up about their backstory?

What is the oldest instrument ever?

How could a civilization detect tachyons?

Why did Ham the Chimp push levers?

why it is 2>&1 and not 2>>&1 to append to a log file

How to append code verbatim to .bashrc?

How can one see if an address is multisig?

Two (probably) equal real numbers which are not proved to be equal?

Identity of a supposed anonymous referee revealed through "Description" of the report



How to fix runtime error while loading data from database “An object or column name is missing or empty.”


How to fix “Referenced assembly does not have a strong name” error?How can I get column names from a table in SQL Server?An object or column name is missing or emptydon't see the new database and can't log inHow to change column datatype in SQL database without losing dataExporting Large Amounts of DataAn object or column name is missing or empty (0x80131904)An object or column name is missing or empty GEOMETRYFill Dataset AsyncRetrieving the data from the database based on the parameter






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















This is the error I get :




Additional information: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.




when running this code; it is supposed to load data from SQL Server to Winforms



private void click(int y)

Edit();
clear_invo();

SqlConnection con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True");

string tname = "";

if (str == "Pur")

tname = "PurchaseT";

else if (str == "GRO")

tname = "Gro_Chln_T";

else if (str == "Gri-Chln_T")

tname = "GRI";

else if (str == "Job")

tname = "JobT";


string sql1 = "select * from [" + tname + "]";

SqlDataAdapter sda1 = new SqlDataAdapter(sql1, con);
DataSet dsi = new DataSet();

con.Open();
sda1.Fill(dsi);

int bene_id = -1;

try

idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

catch (Exception ex)

if (!(idtxt.Text == null))

MessageBox.Show(ex.Message);



con.Close();

load_bene(bene_id);

string challan_id =str+"-"+ idtxt.Text;
Load_item(challan_id);

No_Edit();










share|improve this question



















  • 4





    Are you sure tname has a value when you concat that to make your sql1 string? Put a breakpoint right there and inspect the value of tname in the debugger.

    – rene
    Mar 23 at 7:54











  • Adding to the comment by @rene, you will get this error when you try to execute select * from [].

    – Dan Guzman
    Mar 23 at 9:51

















1















This is the error I get :




Additional information: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.




when running this code; it is supposed to load data from SQL Server to Winforms



private void click(int y)

Edit();
clear_invo();

SqlConnection con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True");

string tname = "";

if (str == "Pur")

tname = "PurchaseT";

else if (str == "GRO")

tname = "Gro_Chln_T";

else if (str == "Gri-Chln_T")

tname = "GRI";

else if (str == "Job")

tname = "JobT";


string sql1 = "select * from [" + tname + "]";

SqlDataAdapter sda1 = new SqlDataAdapter(sql1, con);
DataSet dsi = new DataSet();

con.Open();
sda1.Fill(dsi);

int bene_id = -1;

try

idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

catch (Exception ex)

if (!(idtxt.Text == null))

MessageBox.Show(ex.Message);



con.Close();

load_bene(bene_id);

string challan_id =str+"-"+ idtxt.Text;
Load_item(challan_id);

No_Edit();










share|improve this question



















  • 4





    Are you sure tname has a value when you concat that to make your sql1 string? Put a breakpoint right there and inspect the value of tname in the debugger.

    – rene
    Mar 23 at 7:54











  • Adding to the comment by @rene, you will get this error when you try to execute select * from [].

    – Dan Guzman
    Mar 23 at 9:51













1












1








1








This is the error I get :




Additional information: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.




when running this code; it is supposed to load data from SQL Server to Winforms



private void click(int y)

Edit();
clear_invo();

SqlConnection con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True");

string tname = "";

if (str == "Pur")

tname = "PurchaseT";

else if (str == "GRO")

tname = "Gro_Chln_T";

else if (str == "Gri-Chln_T")

tname = "GRI";

else if (str == "Job")

tname = "JobT";


string sql1 = "select * from [" + tname + "]";

SqlDataAdapter sda1 = new SqlDataAdapter(sql1, con);
DataSet dsi = new DataSet();

con.Open();
sda1.Fill(dsi);

int bene_id = -1;

try

idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

catch (Exception ex)

if (!(idtxt.Text == null))

MessageBox.Show(ex.Message);



con.Close();

load_bene(bene_id);

string challan_id =str+"-"+ idtxt.Text;
Load_item(challan_id);

No_Edit();










share|improve this question
















This is the error I get :




Additional information: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.




when running this code; it is supposed to load data from SQL Server to Winforms



private void click(int y)

Edit();
clear_invo();

SqlConnection con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True");

string tname = "";

if (str == "Pur")

tname = "PurchaseT";

else if (str == "GRO")

tname = "Gro_Chln_T";

else if (str == "Gri-Chln_T")

tname = "GRI";

else if (str == "Job")

tname = "JobT";


string sql1 = "select * from [" + tname + "]";

SqlDataAdapter sda1 = new SqlDataAdapter(sql1, con);
DataSet dsi = new DataSet();

con.Open();
sda1.Fill(dsi);

int bene_id = -1;

try

idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

catch (Exception ex)

if (!(idtxt.Text == null))

MessageBox.Show(ex.Message);



con.Close();

load_bene(bene_id);

string challan_id =str+"-"+ idtxt.Text;
Load_item(challan_id);

No_Edit();







c# sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 7:59









marc_s

588k13111281276




588k13111281276










asked Mar 23 at 7:46









Z3esha9Z3esha9

83




83







  • 4





    Are you sure tname has a value when you concat that to make your sql1 string? Put a breakpoint right there and inspect the value of tname in the debugger.

    – rene
    Mar 23 at 7:54











  • Adding to the comment by @rene, you will get this error when you try to execute select * from [].

    – Dan Guzman
    Mar 23 at 9:51












  • 4





    Are you sure tname has a value when you concat that to make your sql1 string? Put a breakpoint right there and inspect the value of tname in the debugger.

    – rene
    Mar 23 at 7:54











  • Adding to the comment by @rene, you will get this error when you try to execute select * from [].

    – Dan Guzman
    Mar 23 at 9:51







4




4





Are you sure tname has a value when you concat that to make your sql1 string? Put a breakpoint right there and inspect the value of tname in the debugger.

– rene
Mar 23 at 7:54





Are you sure tname has a value when you concat that to make your sql1 string? Put a breakpoint right there and inspect the value of tname in the debugger.

– rene
Mar 23 at 7:54













Adding to the comment by @rene, you will get this error when you try to execute select * from [].

– Dan Guzman
Mar 23 at 9:51





Adding to the comment by @rene, you will get this error when you try to execute select * from [].

– Dan Guzman
Mar 23 at 9:51












1 Answer
1






active

oldest

votes


















1














I would add a default "else" to the end of your if statement. If you hit that then you have your answer...You're not meeting the if statement conditions.



Also, consider swapping that if statement for a switch statement.



UPDATE #1



Here's your function re-written with a switch statement and throwing a custom exception. I've also taken the liberty of refactoring your DB code and also implemented string interpolation.



Just to clarify as per previous comments, the issue looks as though your IF...ELSE statement is insufficient. The value of "str" is equal to something other than "Pur", "GRO", "Gri-Chln-T" or "Job" and so when none of these are matched, your "tname" variable still has no value.



You then use this empty variable to construct your SELECT query which equates to "SELECT * FROM []".



As a side note, I would also recommend not returning * from a query unless you absolutely require every row in the table AND every column in that table.



public class TableIdentificationException : Exception



private void click(int y)

Edit();
clear_invo();

var tname = string.Empty;

switch (str)

case "Pur":
tname = "PurchaseT";
break;
case "GRO":
tname = "Gro_Chln_T";
break;
case "Gri-Chln_T":
tname = "GRI";
break;
case "Job":
tname = "JobT";
break;
default:
var ex = new TableIdentificationException();
ex.Data.Add("LookupString", str);
throw ex;


var bene_id = -1;

using (var con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True"))

var sql1 = $"select * from [tname]";

using (var sda1 = new SqlDataAdapter(sql1, con))

var dsi = new DataSet();

sda1.Fill(dsi);

try

idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

catch (Exception ex)

if (idtxt.Text != null) MessageBox.Show(ex.Message);




load_bene(bene_id);

var challan_id = $"str-idtxt.Text";

Load_item(challan_id);

No_Edit();






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311713%2fhow-to-fix-runtime-error-while-loading-data-from-database-an-object-or-column-n%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I would add a default "else" to the end of your if statement. If you hit that then you have your answer...You're not meeting the if statement conditions.



    Also, consider swapping that if statement for a switch statement.



    UPDATE #1



    Here's your function re-written with a switch statement and throwing a custom exception. I've also taken the liberty of refactoring your DB code and also implemented string interpolation.



    Just to clarify as per previous comments, the issue looks as though your IF...ELSE statement is insufficient. The value of "str" is equal to something other than "Pur", "GRO", "Gri-Chln-T" or "Job" and so when none of these are matched, your "tname" variable still has no value.



    You then use this empty variable to construct your SELECT query which equates to "SELECT * FROM []".



    As a side note, I would also recommend not returning * from a query unless you absolutely require every row in the table AND every column in that table.



    public class TableIdentificationException : Exception



    private void click(int y)

    Edit();
    clear_invo();

    var tname = string.Empty;

    switch (str)

    case "Pur":
    tname = "PurchaseT";
    break;
    case "GRO":
    tname = "Gro_Chln_T";
    break;
    case "Gri-Chln_T":
    tname = "GRI";
    break;
    case "Job":
    tname = "JobT";
    break;
    default:
    var ex = new TableIdentificationException();
    ex.Data.Add("LookupString", str);
    throw ex;


    var bene_id = -1;

    using (var con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True"))

    var sql1 = $"select * from [tname]";

    using (var sda1 = new SqlDataAdapter(sql1, con))

    var dsi = new DataSet();

    sda1.Fill(dsi);

    try

    idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
    bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

    catch (Exception ex)

    if (idtxt.Text != null) MessageBox.Show(ex.Message);




    load_bene(bene_id);

    var challan_id = $"str-idtxt.Text";

    Load_item(challan_id);

    No_Edit();






    share|improve this answer





























      1














      I would add a default "else" to the end of your if statement. If you hit that then you have your answer...You're not meeting the if statement conditions.



      Also, consider swapping that if statement for a switch statement.



      UPDATE #1



      Here's your function re-written with a switch statement and throwing a custom exception. I've also taken the liberty of refactoring your DB code and also implemented string interpolation.



      Just to clarify as per previous comments, the issue looks as though your IF...ELSE statement is insufficient. The value of "str" is equal to something other than "Pur", "GRO", "Gri-Chln-T" or "Job" and so when none of these are matched, your "tname" variable still has no value.



      You then use this empty variable to construct your SELECT query which equates to "SELECT * FROM []".



      As a side note, I would also recommend not returning * from a query unless you absolutely require every row in the table AND every column in that table.



      public class TableIdentificationException : Exception



      private void click(int y)

      Edit();
      clear_invo();

      var tname = string.Empty;

      switch (str)

      case "Pur":
      tname = "PurchaseT";
      break;
      case "GRO":
      tname = "Gro_Chln_T";
      break;
      case "Gri-Chln_T":
      tname = "GRI";
      break;
      case "Job":
      tname = "JobT";
      break;
      default:
      var ex = new TableIdentificationException();
      ex.Data.Add("LookupString", str);
      throw ex;


      var bene_id = -1;

      using (var con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True"))

      var sql1 = $"select * from [tname]";

      using (var sda1 = new SqlDataAdapter(sql1, con))

      var dsi = new DataSet();

      sda1.Fill(dsi);

      try

      idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
      bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

      catch (Exception ex)

      if (idtxt.Text != null) MessageBox.Show(ex.Message);




      load_bene(bene_id);

      var challan_id = $"str-idtxt.Text";

      Load_item(challan_id);

      No_Edit();






      share|improve this answer



























        1












        1








        1







        I would add a default "else" to the end of your if statement. If you hit that then you have your answer...You're not meeting the if statement conditions.



        Also, consider swapping that if statement for a switch statement.



        UPDATE #1



        Here's your function re-written with a switch statement and throwing a custom exception. I've also taken the liberty of refactoring your DB code and also implemented string interpolation.



        Just to clarify as per previous comments, the issue looks as though your IF...ELSE statement is insufficient. The value of "str" is equal to something other than "Pur", "GRO", "Gri-Chln-T" or "Job" and so when none of these are matched, your "tname" variable still has no value.



        You then use this empty variable to construct your SELECT query which equates to "SELECT * FROM []".



        As a side note, I would also recommend not returning * from a query unless you absolutely require every row in the table AND every column in that table.



        public class TableIdentificationException : Exception



        private void click(int y)

        Edit();
        clear_invo();

        var tname = string.Empty;

        switch (str)

        case "Pur":
        tname = "PurchaseT";
        break;
        case "GRO":
        tname = "Gro_Chln_T";
        break;
        case "Gri-Chln_T":
        tname = "GRI";
        break;
        case "Job":
        tname = "JobT";
        break;
        default:
        var ex = new TableIdentificationException();
        ex.Data.Add("LookupString", str);
        throw ex;


        var bene_id = -1;

        using (var con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True"))

        var sql1 = $"select * from [tname]";

        using (var sda1 = new SqlDataAdapter(sql1, con))

        var dsi = new DataSet();

        sda1.Fill(dsi);

        try

        idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
        bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

        catch (Exception ex)

        if (idtxt.Text != null) MessageBox.Show(ex.Message);




        load_bene(bene_id);

        var challan_id = $"str-idtxt.Text";

        Load_item(challan_id);

        No_Edit();






        share|improve this answer















        I would add a default "else" to the end of your if statement. If you hit that then you have your answer...You're not meeting the if statement conditions.



        Also, consider swapping that if statement for a switch statement.



        UPDATE #1



        Here's your function re-written with a switch statement and throwing a custom exception. I've also taken the liberty of refactoring your DB code and also implemented string interpolation.



        Just to clarify as per previous comments, the issue looks as though your IF...ELSE statement is insufficient. The value of "str" is equal to something other than "Pur", "GRO", "Gri-Chln-T" or "Job" and so when none of these are matched, your "tname" variable still has no value.



        You then use this empty variable to construct your SELECT query which equates to "SELECT * FROM []".



        As a side note, I would also recommend not returning * from a query unless you absolutely require every row in the table AND every column in that table.



        public class TableIdentificationException : Exception



        private void click(int y)

        Edit();
        clear_invo();

        var tname = string.Empty;

        switch (str)

        case "Pur":
        tname = "PurchaseT";
        break;
        case "GRO":
        tname = "Gro_Chln_T";
        break;
        case "Gri-Chln_T":
        tname = "GRI";
        break;
        case "Job":
        tname = "JobT";
        break;
        default:
        var ex = new TableIdentificationException();
        ex.Data.Add("LookupString", str);
        throw ex;


        var bene_id = -1;

        using (var con = new SqlConnection(@"Data Source=ANSARI-PC;Initial Catalog=BMS;Integrated Security=True"))

        var sql1 = $"select * from [tname]";

        using (var sda1 = new SqlDataAdapter(sql1, con))

        var dsi = new DataSet();

        sda1.Fill(dsi);

        try

        idtxt.Text = dsi.Tables[0].Rows[y][0].ToString();
        bene_id = Convert.ToInt32(dsi.Tables[0].Rows[y][1]);

        catch (Exception ex)

        if (idtxt.Text != null) MessageBox.Show(ex.Message);




        load_bene(bene_id);

        var challan_id = $"str-idtxt.Text";

        Load_item(challan_id);

        No_Edit();







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 25 at 15:02

























        answered Mar 23 at 9:00









        Wayne FelthamWayne Feltham

        466313




        466313





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311713%2fhow-to-fix-runtime-error-while-loading-data-from-database-an-object-or-column-n%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

            Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?