0

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

0

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 & "']) "

1 Comment

Not helpful, same as it was earlier :-(
0

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

2 Comments

Can you debug the program and provide the value of cmd please?
I found the mistake, there was space in between the table name. That's why it showing the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.