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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현