dnn linqtosql对象引用未设置为对象异常的实例

时间:2012-01-23 14:25:21

标签: c# asp.net linq-to-sql dotnetnuke

我有这样的代码试图将一些信息保存到数据库但是我的datacontext无法创建。它给出了一个例外“对象引用没有设置为对象的实例。”当我调试时,它跳转到“DataContext1 db = new DataContext1();” line to catch block并给出异常。这有什么解决方案吗?

public class AuthorPaperDetails
{
    public void SaveAuthorPaperDetails(string pTitle, string confMakerId, 
                                                string additionalPaperTitle,string mainAuthor, 
                                                int regFeeForFirstAuthor,int regFeeForAdditionalPaper, int RegFeeForCoAuthors)
    {

        try
        {
            DataContext1 db = new DataContext1();
            AuthorPaperDetail authorPaperDetail = new AuthorPaperDetail();

            authorPaperDetail.paper_title = pTitle;
            authorPaperDetail.conference_maker_id = confMakerId;
            authorPaperDetail.additional_paper_title = additionalPaperTitle;
            authorPaperDetail.areYouMainAuthor = mainAuthor;
            authorPaperDetail.feeForFirstAuthorPaper = regFeeForFirstAuthor;
            authorPaperDetail.feeForAdditionalPaper = regFeeForAdditionalPaper;
            authorPaperDetail.feeForParticipCoAuthors = RegFeeForCoAuthors;

            db.AuthorPaperDetails.InsertOnSubmit(authorPaperDetail);
            db.SubmitChanges();
        }
        catch (Exception)
        {
        }
    }
}

1 个答案:

答案 0 :(得分:2)

创建dbml时是不是保存了连接信息,还是需要将连接信息传递给DataContext,如下所示:

TestDataContext dc = new TestDataContext("ConnectionString1");