-3

What I'm missing?
I've got error:

Sqlexception was unhandled by usercode

SqlConnection con = new SqlConnection(MyConnectionString);
SqlCommand objCmd;
con.Open();
SqlDataReader dtReader;
String strSQL;
strSQL = "SELECT * FROM " + DropDownList1.SelectedValue + "'";

objCmd = new SqlCommand(strSQL, con);
dtReader = objCmd.ExecuteReader();

//*** BindData to GridView ***//
GridView3.DataSource = dtReader;
GridView3.DataBind();

dtReader.Close();
dtReader = null; 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

Try this ,

  strSQL = "SELECT * FROM " + DropDownList1.SelectedValue;

ie., just remove ' from the query string.

Then add the following code at the end.

GridView3.DataSource = dtReader;
GridView3.DataSourceID = String.Empty;
GridView3.DataBind(); 
itzmebibin
  • 9,199
  • 8
  • 48
  • 62
0

this code works now

  SqlConnection con = new SqlConnection(MyConnectionString);
SqlCommand objCmd;
con.Open();
SqlDataReader dtReader;
String strSQL;
strSQL = "SELECT * FROM " + DropDownList1.SelectedValue ;

objCmd = new SqlCommand(strSQL, con);
dtReader = objCmd.ExecuteReader();

GridView3.DataSource = dtReader;
GridView3.DataSourceID = String.Empty;
GridView3.DataBind(); 

dtReader.Close();
dtReader = null; 
jaypeagi
  • 3,133
  • 18
  • 33