Piyush
Piyush

Reputation: 886

How do I store source code lines in SQL Server Database and access it via DataSet

I want to store the following code into the database:

fun(...);

int main()
{
    fun(3, 7, -11.2, 0.66);
    return 0;
}
fun(...)
{
    va_list ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}

and then get it back in the dataset and display on a page.

As of now I have successfully stored the questions with varchar(MAX) datatype but when I try to get it in the dataset i get the following error:

Failed To Enable Constraints. One Or More Rows Contain Values Violating Non-null, Unique, Or Foreign-key Constraints.

I am doing this in a ASP.NET web application.

EDIT: Here is the Table definition of the table I am inserting the data into

Database Definition

The query I am using to insert the data into the table:

con.ConnectionString = constr;
    cmd.Connection = con;
    cmd.CommandText = "insert into QuesTable values(@D1,@D2,@D3,@D4,@D5,@D6,@D7, NULL)";
    cmd.Parameters.Add("@D1", SqlDbType.Int).Value = txtQID.Text;
    cmd.Parameters.Add("@D2", SqlDbType.VarChar).Value = txtques.Text;
    cmd.Parameters.Add("@D3", SqlDbType.VarChar).Value = txtansa.Text;
    cmd.Parameters.Add("@D4", SqlDbType.VarChar).Value = txtansb.Text;
    cmd.Parameters.Add("@D5", SqlDbType.VarChar).Value = txtansc.Text;
    cmd.Parameters.Add("@D6", SqlDbType.VarChar).Value = txtansd.Text;
    cmd.Parameters.Add("@D7", SqlDbType.VarChar).Value = txtcorr.Text;

    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();

And finally the code by which I am extracting the data from the dataset

DataSet1.QuesTableDataTable dt = new DataSet1.QuesTableDataTable();
    DataSet1TableAdapters.QuesTableTableAdapter adp = new DataSet1TableAdapters.QuesTableTableAdapter();
    dt = adp.GetData();
    DataTable dtUser = dt.Clone();

Hope the information is helpful.

Upvotes: 1

Views: 1251

Answers (2)

Dave
Dave

Reputation: 3621

Since I can't see if you've got any other further constraints on the table, it looks like the value you're inserting into the primary key field (Qid) already exists in the table.

If you need to create a new row for every entry regardless, it would probably be easier to change the column Qid to maintain its own Identity. If you need to update an existing value, you'll need to add a separate piece of logic to determine if the primary key value already exists and update or insert accordingly.

Upvotes: 1

Brian Mains
Brian Mains

Reputation: 50728

That's an error related to either:

  • A not null field is having a null passed in when you load the data.
  • A foreign key or unique index check is invalid (for instance, FK value of 9 doesn't exist).

If you fill the dataset, the error that is generated should be retrievable from the dataset.

HTH.

Upvotes: 0

Related Questions