简体   繁体   中英

How to resolve Invalid Object Name “Table Name” in C#?

When I am running My application in Visual Studio 2008 it is showing me Message Box with notation of Invalid Object Name "Table name", instead the table is already there in my database. So please help me to solve this problem.

private void FrmInwardDisp2_Load(object sender, EventArgs e)
{
  byte _true = 1;
  byte _false = 0;
  //this.toolTip1.SetToolTip(this.dgvDisplay, "Double Click to Edit");
  int width = Screen.PrimaryScreen.Bounds.Width;
  int height = Screen.PrimaryScreen.Bounds.Height - 175;
  dgvDisplay.Width = width - 25;
  dgvDisplay.Height = height;
  //dgvDisplay.AutoSize = true;

  try
  {
    SqlConnection con = new SqlConnection(Variables.con);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.SelectCommand = cmd;

    switch (Static_Class.selected)
    {
      case 1:
      {
         btnAddInward.Text = "Add Rental Inward";
         cmd.CommandText = "SELECT [RentalInwardNo],[VoucherNo],[NameOfWork]
         [Location],[NameOfVendor],[VendorCode],[VendorItemCode],[ItemName],
         [Narration],[WareHouse],[DriverName],[VehicleNo],[RejectedItemCode],  
         [RejectedItemName],[Rate],[Date],[InvoiceNo],[ChallanNo],[TenderCode],
         [PINo],[PONo],[UnitQty],[UOM],[RequisitionNo],[ReceivedBy],
         [LocationID],[Layer],[Local],[Global],[DeleteStatus],[AutoGenerated] 
         FROM IN_Rental_Inward 
         where [TenderCode]= " + Static_Class.tendercode + " 
         and [LocationID]='" + Static_Class.LocationID + "' 
         and [Layer]='" + Static_Class.Layer + "' 
         and [DeleteStatus]=" + 0;

         try
         {
           //con.Open();
           da.Fill(ds, "TableDisplay");
           da.Dispose();
           cmd.Dispose();
           //con.Close();
           dgvDisplay.DataSource = ds.Tables["TableDisplay"];

         }
         catch (Exception ex)
         {
           MessageBox.Show(ex.Message);
         }
      }
      break;

In your query text, specify the table's schema name also before the table name. For example:

cmd.CommandText = "SELECT … FROM dbo.IN_Rental_Inward …";
//                               ^^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM