Passing Datatable Parameter to Stored Procedure

Oct 8 2011 3:07 PM
Hello,
I am facing some problem in passing data table parameter to stored procedure.
There are two table 
tblQuestion and tblAnswerChoice
1. tblQuestion is for storing questions and its columns are
a)Qid int primary key identity
b)Question ntext
c)Createiondate datetime
2. tblAnswerChoice is for storing Answer Choices for the particular question and its columns are
a)Ansid int primary key identity
b)qid int 
c)AnswerChoice ntext
User is free to add any number of answer choice but he must add a minimum of two.
Below is the code I am using to store the data in the database
 

protected void btnSubmit_Click(object sender, EventArgs e)
    {
                DataTable dataTable = new DataTable("SampleDataType");
        //we create column names as per the type in DB
        dataTable.Columns.Add("AnswerChoice", typeof(string));
        if (Session["Panel"] != null)
        {
            for (int i = 1; i <= count; i++)
            {
                if (Request.Form["txtChoice" + i] != string.Empty)
                {
                    dataTable.Rows.Add(Request.Form["txtChoice_" + i]);                }
            }
        }
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        cmd = new SqlCommand();
        cmd.Connection = conn;
       
        cmd.CommandText = "sp_save_user_quiz_questions";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Question", txtQuestion.Text);
        cmd.Parameters.AddWithValue("@CreationDate", DateTime.Now);
       
        cmd.Parameters.AddWithValue("@AnswerChoice1", txtChoice1.Text);
        cmd.Parameters.AddWithValue("@AnswerChoice2", txtChoice2.Text);
        SqlParameter parameter = new SqlParameter();
        //The parameter for the SP must be of SqlDbType.Structured
        parameter.ParameterName = "@Sample";
        parameter.SqlDbType = System.Data.SqlDbType.Structured;
        parameter.Value = dataTable;
        cmd.Parameters.Add(parameter);    
     
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
        }
        cmd.Dispose();
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
}
Now about the problem
I stored the answerchoices created by dynamic text box in data table. And I am passing the data table as a parameter to stored procedure. 
But I am facing the problem in writing stored procedure to store the data which are is the data table. I am using SQL Server 2008. As I checked in Google my problem can be solved by Table data type. But how can I use this to solve my problem?
And one thing to note is that the qid which is generated during saving the data in tblQuestion is stored in tblAnswerChoice. So we have to fire queries for both table at one time in stored procedure.
Because at the same time data is stored in both table.
Thanks,
Deepak

Answers (1)