Rahul Lakshakar

Rahul Lakshakar

  • NA
  • 199
  • 41.7k

Create table in database on button click

Oct 22 2016 12:52 AM
I am creating an system using Asp.Net c#. I want create a table in the database on submit the details. I am taking the name of the table from dropdown selected item. I am using the following code but it is not working please help me..... 
 
string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();


string insertData = "insert into ExamDetail (ExamName, SubjectName, Duration, StartTime, EndTime, NegativeMark, QusNo, QusMarks) values (@EName, @SubName, @DUR, @ST, @ET, @NM, @QN,@QM)";
SqlCommand com = new SqlCommand(insertData, con);

com.Parameters.AddWithValue("@EName", DropExamName.SelectedItem.Text);
com.Parameters.AddWithValue("@SubName", DropSubName.SelectedItem.Text);
com.Parameters.AddWithValue("@DUR", DropDuration.SelectedItem.Text);
com.Parameters.AddWithValue("@ST", Text_Stime.Text);
com.Parameters.AddWithValue("@ET", Text_Etime.Text);
com.Parameters.AddWithValue("@NM", RadioNegativeMarks.SelectedItem.Text);
com.Parameters.AddWithValue("@QN", Text_question_no.Text);
com.Parameters.AddWithValue("@QM", text_question_marks.Text);

string createExamTable = "BEGIN";
createExamTable += "CREATE TABLE [dbo]. ['"+ DropExamName.SelectedItem.Text +"'](";
createExamTable += "[ExamId] INT IDENTITY(1,1) NOT NULL,";
createExamTable += "[QusNo] INT NOT NULL,";
createExamTable += "[QusDetail] VARCHAR(MAX) NOT NULL";
createExamTable += "[Optn1] VARCHAR(MAX) NOT NULL";
createExamTable += "[Optn2] VARCHAR(MAX) NOT NULL";
createExamTable += "[Optn3] VARCHAR(MAX) NOT NULL";
createExamTable += "[Optn4] VARCHAR(MAX) NOT NULL";
createExamTable += "[CorrectOptn] INT NOT NULL";
createExamTable += ")";
createExamTable += " END";

SqlCommand ComCreateTable = new SqlCommand(createExamTable, con);
com.ExecuteNonQuery();
ComCreateTable.ExecuteNonQuery();
Response.Write("DONE");
 

Answers (4)