Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:01:05+00:00 2026-06-08T11:01:05+00:00

Dynamically create table and insert data into the created table, I’m using following command

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:01:07+00:00Added an answer on June 8, 2026 at 11:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to create dynamically tag inside table. At first create link then inside link
I was trying to create a table dynamically and put textboxes in it. Here
Can we create a temporary table from the stored procedure results dynamically? I do
I have a dynamically created table which in the last <td> there is a
I have a table which is dynamically created based on the amount of results
i have a table which contains a bunch of dynamically created radio button lists,
I have two columns in my table and the rows are created dynamically. <table
I've created a Javascript object via prototyping. I'm trying to render a table dynamically.
Am attempting to dynamically create html which then injects itself as an iframe into
I want to insert the data in a SQLite table login when a button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.