使用LINQ to SQL随机获取Object引用未设置为对象奇怪行为的实例

时间:2014-11-15 21:52:35

标签: c# wpf linq

我正在处理我的WPF应用程序,最近我的客户告诉我,当他们想要将新客户端添加到应用程序中时,他们会随机。该应用程序是关于添加和管理客户端。

问题是我没有这个错误在我的VS上也无法解决它!

代码在这里这是我的插入按钮,用于将客户端详细信息添加到数据库中。

我正在使用大量的try和catch块来捕获此错误,但对我来说没有任何内容

我有这个标志,也可以使用插入按钮来保存编辑。

如果flag = 0将插入else去编辑

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        try
        {
            if (Class1.flag == 0)
            {


                    DataClasses1DataContext db5 = new DataClasses1DataContext();
                    tbl_Gharar g = new tbl_Gharar
                    {




                        id = Convert.ToInt32(txt_Numgh.Text),
                        gh_Name = txt_Ghname.Text,
                        date_Sabt = txt_date2.Text,
                        date_Want = txt_Clientdate.Text,
                        list_Khadamat = txt_List.Text,
                        tozih = txt_Tozih.Text,
                        final_Price = Decimal.Parse(txt_Hazine.Text != String.Empty ? txt_Hazine.Text : "0"),
                        daryafti = txt_Daryafti.Text,
                        tell_hamrah = txt_tellhamrah.Text,
                        tell_home = txt_tellhome.Text,
                        gh_Pic = txt_picm.Text,
                        gh_Picwork = txt_picwork.Text,
                        //   type = lst_type.SelectedItems.ToString()

                    };


                    db5.tbl_Gharars.InsertOnSubmit(g);
                    db5.SubmitChanges();
                    MessageBoxResult mres = PersianMessageBox_Mhclass.Show("saved", "info", PersianMessageBox_Mhclass.MsgMhButton.OK, PersianMessageBox_Mhclass.MsgMhIcon.Information);
                    this.Close();
                    var mainwin = System.Windows.Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
                    mainwin.mytabcont.SelectedIndex = 2;

            }

            else
            {

                try
                {
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    var qedit = db.tbl_Gharars.Where(c => c.id == id).SingleOrDefault();
                    qedit.id = Convert.ToInt32(txt_Numgh.Text);
                    qedit.gh_Name = txt_Ghname.Text;
                    qedit.date_Sabt = txt_date2.Text;
                    qedit.date_Want = txt_Clientdate.Text;
                    qedit.list_Khadamat = txt_List.Text;
                    qedit.tozih = txt_Tozih.Text;
                    qedit.final_Price = Convert.ToDecimal(txt_Hazine.Text);
                    qedit.daryafti = txt_Daryafti.Text;
                    qedit.tell_home = txt_tellhome.Text;
                    qedit.tell_hamrah = txt_tellhamrah.Text;
                    qedit.gh_Pic = txt_picm.Text;
                    qedit.gh_Picwork = txt_picwork.Text;

                    db.SubmitChanges();
                    MessageBoxResult mres = PersianMessageBox_Mhclass.Show("edit ", "info", PersianMessageBox_Mhclass.MsgMhButton.OK, PersianMessageBox_Mhclass.MsgMhIcon.Information);
                }


                catch (NullReferenceException ex)
                {
                    System.Windows.MessageBox.Show("cant find anything",ex.Message);
                }
                Class1.flag = 0;
                this.Close();
            }
        }
        catch
        {

        }

    }

0 个答案:

没有答案