Hi all..i have some trouble to insert data into 2 table.. i use asp.net with C# language..this is my table :

DOCUMENT
id_document
name
description

VERSI
Id_versi
Id_document
url_file
size
type
modifiedon

relationship of document and versi is one to many..
this is my code, but i cant store anything..
can u help me to correct my code, please??

protected void Button_Upload_Click(object sender, EventArgs e)
    {
        try
        {
            DMSDataAccess db = new DMSDataAccess();
            DOCUMENT doc = new DOCUMENT();
            VERSI versi = new VERSI();

            doc.name = TextBox_Form_Judul.Text;
            doc.description = TextBox_Form_Deskripsi.Text;
            db.DMSDataContext.DOCUMENTs.InsertOnSubmit(doc);
            db.DMSDataContext.SubmitChanges();
            if (FileUpload1.HasFile)
            {       
                    string strFileName = FileUpload1.FileName.ToString();
                    string strExtension = Path.GetExtension(strFileName);
                    string strTimeStamp = DateTime.Now.ToString("yyyyMMdd-HHmmss.fff");
                    string strName = Path.GetFileNameWithoutExtension(strFileName);
                    strFileName = strName + "-" + strTimeStamp + strExtension;
                    this.FileUpload1.SaveAs(Server.MapPath(@"~\\Staf\\Uploads\\" + strFileName.ToString()));
                    Label_Upload_Response.Text = "Upload file berhasil!";
                    double size_file = FileUpload1.PostedFile.ContentLength;
                    string type_file = FileUpload1.PostedFile.ContentType;

                    versi.IdDocument = doc.IdDocument;
                    versi.UrlFile = "Uploads/" + strFileName;
                    versi.Size = size_file;
                    versi.Type = type_file;
                    versi.ModifiedOn = DateTime.Now;

                    db.DMSDataContext.VERSIs.InsertOnSubmit(versi);
                    db.DMSDataContext.SubmitChanges();
                }               
        }
        catch 
        {
        }

Recommended Answers

All 3 Replies

db.DMSDataContext.DOCUMENTs.InsertOnSubmit(new DOCUMENT()
        {
            name = TextBox_Form_Judul.Text,
            description = TextBox_Form_Deskripsi.Text,
            VERSIs = new System.Data.Linq.EntitySet<VERSI>()
            {
                 new VERSI() 
                   { 
                    UrlFile = "Uploads/" + strFileName;
                    Size = size_file;
                    Type = type_file;
                    ModifiedOn = DateTime.Now;
                    }
                  
            }

        });

 db.DMSDataContext.SubmitChanges();

PS: I haven't tested.

i found some error in your code.. this code cannot help me to find solution.. do u have anything else? (another solution)
can u help me?

kieky : could you debug and check if there is any SQLException that is unhandled during an insert, code seems okay.

  1. remove the lines 12 and 32,
    "db.DMSDataContext.SubmitChanges();"
    and add it in between 33 and 34.

2.1. Ensure you have a transaction present in your insert query, which will check if Doc is inserted first and next versi, only if doc has been inserted.

or

2.2. Add the insert queries to a stored proc, so that at one statement you can achieve the output reducing the code behind code in .cs file.

_avd : appreciate your code, its nice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.