-1

I am trying to bind database table with data grid but I am facing issue in Line 35 in the code sda.fill(dt); but I am getting an exception in Line 35

Incorrect Syntax near '.'

Code:

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(@"Data Source=192.168.0.61\newsql;Initial Catalog=AIETraining;User ID=AIETrainingAccount;Password=Training@1234");

            cn.Open();
            Response.Write("Connection established");

            SqlCommand command = new SqlCommand("Select * from Gursimran 
            Singh.publishers", cn);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5] { 
                       new DataColumn("pub_id", typeof(int)),
                       new DataColumn("pub_name",typeof(string)),
                       new DataColumn("city",typeof(string)),
                       new DataColumn("state",typeof(string)),
                       new DataColumn("Country",typeof(string))});

            SqlDataAdapter sda = new SqlDataAdapter();

            using (command)
            {
                using (sda) 
                {
                     command.Connection = cn;
                     sda.SelectCommand = command;

                     using(dt)
                     {
                          sda.Fill(dt);

                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                     }
                }
            }

            cn.Close();
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 5
    What is `Gursimran Singh.publishers`? There's errant whitespace in that. Do you have an object name in your schema with whitespace in it? Something else? – David May 17 '19 at 11:01
  • If you do really have a schema called `Gursimran Singh` then the space will break your query. Wrap the schema with square brackets... `[Gursimran Singh]` – Reinstate Monica Cellio May 17 '19 at 11:01
  • Ohh hell yeah .. I got it .. By the way .. Thanks for assistance –  May 17 '19 at 11:04
  • Have you tried write sql command with escape character like `@"Select * from Gursimran Singh.publishers"` – Hasan Gholamali May 17 '19 at 11:05

1 Answers1

2

Try Below

Deal with SQL keywords/Space

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(@"Data Source=192.168.0.61\newsql;Initial Catalog=AIETraining;User ID=AIETrainingAccount;Password=Training@1234");

            cn.Open();
            Response.Write("Connection established");

            SqlCommand command = new SqlCommand("Select * from [Gursimran Singh].[publishers]", cn);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5] { 
                       new DataColumn("pub_id", typeof(int)),
                       new DataColumn("pub_name",typeof(string)),
                       new DataColumn("city",typeof(string)),
                       new DataColumn("state",typeof(string)),
                       new DataColumn("Country",typeof(string))});

            SqlDataAdapter sda = new SqlDataAdapter();

            using (command)
            {
                using (sda) 
                {
                     command.Connection = cn;
                     sda.SelectCommand = command;

                     using(dt)
                     {
                          sda.Fill(dt);

                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                     }
                }
            }

            cn.Close();
        }
    }
}
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115