Ravi
Ravi

Reputation: 31437

Dynamically create table and insert data in asp.net

Dynamically create table and insert data into the created table, I'm using following command to create a table dynamically and insert the values. I'm commented the sample value for the variable respectively.

Dim question, opt1, opt2, opt3, opt4, ans, paper As String
          question = Request.QueryString("question") '<p>This is my first question</p>
          opt1 = Request.QueryString("option1") ' option 1
          opt2 = Request.QueryString("option2") ' option 2
          opt3 = Request.QueryString("option3") ' option 3
          opt4 = Request.QueryString("option4") ' option 4
          ans = Request.QueryString("answer")    ' 2
          paper = Request.QueryString("paper")  ' cs-5-cder-2012
          'Response.Write("....")
          Dim cs As String = ConfigurationManager.ConnectionStrings("eExamSolutionConnection").ConnectionString
          Dim cn As New SqlConnection(cs)
      Dim cmd As New SqlCommand

      Dim cmdText As String
      cmdText = "IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = '" & paper & "') "
      cmdText = cmdText & " BEGIN"
      cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
      cmdText = cmdText & " END"
      cmdText = cmdText & " ELSE"
      cmdText = cmdText & " BEGIN"
      cmdText = cmdText & " CREATE TABLE " & paper & "(question_Id int AUTO_INCREMENT,question varchar(MAX),option1 varchar(255),option2 varchar(255),option3 varchar(255),option4 varchar(255),answer varchar(2));"
      cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
      cmdText = cmdText & " END"
      Try
          cmd.CommandText = cmdText.ToString
          cmd.Connection = cn
          cmd.Connection.Open()
          cmd.ExecuteNonQuery()
          cmd.Connection.Close()
          cmd.Dispose()
          cn.Dispose()
          Response.Write("Question Added !!")
      Catch ex As Exception
          Response.Write(ex.ToString)
      End Try

But, I'm getting exception

System.Data.SqlClient.SqlException: Incorrect syntax near '-'. Incorrect syntax near '-'. Incorrect syntax near '-'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ASP.admin_addquestion_aspx._Render_control1(HtmlTextWriter __w, Control parameterContainer) in D:\EWA\eExam\admin\addQuestion.aspx:line 40

Upvotes: 0

Views: 1683

Answers (3)

Umesh
Umesh

Reputation: 2742

Hyphen character "-" is not allowed in table name. You must replace it with either underscore "_" or completely remove it.

Upvotes: 0

Vikash Sinha
Vikash Sinha

Reputation: 183

Please use

    Dim cmdText As String
    cmdText = "IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = '" & paper & "') "
    cmdText = cmdText & " BEGIN"
    cmdText = cmdText & " INSERT INTO [" & paper & "](question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
    cmdText = cmdText & " END"
    cmdText = cmdText & " ELSE"
    cmdText = cmdText & " BEGIN"
    cmdText = cmdText & " CREATE TABLE [" & paper & "](question_Id int IDENTITY(1,1),question varchar(MAX),option1 varchar(255),option2 varchar(255),option3 varchar(255),option4 varchar(255),answer varchar(2));"
    cmdText = cmdText & " INSERT INTO [" & paper & "](question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
    cmdText = cmdText & " END"

Instead of

   Dim cmdText As String
  cmdText = "IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = '" & paper & "') "
  cmdText = cmdText & " BEGIN"
  cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
  cmdText = cmdText & " END"
  cmdText = cmdText & " ELSE"
  cmdText = cmdText & " BEGIN"
  cmdText = cmdText & " CREATE TABLE " & paper & "(question_Id int AUTO_INCREMENT,question varchar(MAX),option1 varchar(255),option2 varchar(255),option3 varchar(255),option4 varchar(255),answer varchar(2));"
  cmdText = cmdText & " INSERT INTO " & paper & "(question,option1,option2,option3,option4,answer) VALUES('" & question & "','" & opt1 & "','" & opt2 & "','" & opt3 & "','" & opt4 & "','" & ans & "');"
  cmdText = cmdText & " END"

Problem was:

1 Table name should be[] at create and insert time. 2 For autoincremnet there will be IDENTITY(1,1) in SQL server.

Upvotes: 1

Kinexus
Kinexus

Reputation: 12904

You could try adding [ and ] around the table name, so it would become;

"IF EXISTS (SELECT * FROM   INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = 'RAVI' AND TABLE_NAME = ['" & paper & "']) "

Upvotes: 0

Related Questions