将数据插入SQL Server不起作用,但是没有错误消息显示 [英] inserting data into SQL Server does not work but there is no error message to show

查看:88
本文介绍了将数据插入SQL Server不起作用,但是没有错误消息显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net 4.5和SQL Server 2008 Express.

I am using asp.net 4.5 and SQL Server 2008 Express.

我想在我的数据库中插入表单的数据,连接字符串可以,并且可以在另一个页面中工作,但是在此页面中,插入按钮似乎刷新了页面,没有插入!

I want to insert a form's data into my database, connection string is OK and works in another page but in this page it seems the insert button just refreshes the page and no insert happened!

代码:

 protected void Button1_Click(object sender, EventArgs e)
 {
        var conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        var cmdd = "insert into poroje (Pname,Pmozu,Ppayan,StartDate,EndTime,makan,tozih) values(@Pname,@Pmozu,@Ppayan,@StartDate,@EndTime,@makan,@tozih)";

        using (SqlConnection cnn = new SqlConnection(conn))
        {
            using (SqlCommand cmd = new SqlCommand(cmdd, cnn))
            {
                if (aksporoje.HasFile)
                {
                    if (CheckFileType(aksporoje.FileName))
                    {
                       PersianCalendar Pe = new PersianCalendar();

                       string s = Pe.GetYear(System.DateTime.Now).ToString();

                       if (Directory.Exists("~/poroje/"+s))
                       {
                           Directory.CreateDirectory(Server.MapPath("~/poroje/" + s+pushePoroje(System.DateTime.Today)));
                           string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
                           aksporoje.SaveAs(MapPath(filePath));

                           cmd.Parameters.AddWithValue("@aks","poroje/"+ s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
                       }
                       else {
                           Directory.CreateDirectory(Server.MapPath("~/poroje/"+s));
                           Directory.CreateDirectory(Server.MapPath("~/poroje/"+s+ pushePoroje(System.DateTime.Today)));
                           string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
                           aksporoje.SaveAs(MapPath(filePath));
                           cmd.Parameters.AddWithValue("@aks", "poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
                       }
                   }
                } 
                cmd.Parameters.AddWithValue("@Pname", txtnam.Text);
                cmd.Parameters.AddWithValue("@Pmozu", txtmozu.Text);
                cmd.Parameters.AddWithValue("@Ppayan",false);
                cmd.Parameters.AddWithValue("@StartDate",PersianDateTextBox1.Text);
                cmd.Parameters.AddWithValue("@EndTime",null);
                cmd.Parameters.AddWithValue("@makan", txtmakan.Text);
                cmd.Parameters.AddWithValue("@tozih", txttozih.Text);

                try
                {
                    cnn.Open();

                    int recordsAffected = cmd.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    lblpeygham.Visible = true;
                    lblpeygham.Text = "error";
                }
                finally
                {
                    cnn.Close();
                    lblpeygham.Visible = true;
                    lblpeygham.Text = "data inserted";
                }
            }
        }

当我单击按钮时,页面将刷新并显示已插入数据"消息.但是,当我检查数据库时,什么也没有插入!你能帮我吗?

When I click on button the page refreshes amd the "data inserted" message is shown to me. But when I check the database nothing is inserted! Can you help me please?

推荐答案

谢谢大家!我更改了这部分

thank you all! I change this part

 catch (SqlException)
            {
                lblpeygham.Visible = true;
                lblpeygham.Text = "error";
            }

 catch (SqlException ex)
                {
                    lblpeygham.Visible = true;
                    lblpeygham.Text = ex.message;
                }

并了解到databse中的startDate类型是dateTime,但是我在其中插入了nvarchar!但是现在我遇到了一个新问题.我想用主机中的年份名称创建新文件夹,但是如果我写

and understand that the startDate type in databse is dateTime but i was inserting nvarchar in it! but now i hav a new problem. i want to create new folder with the Year name in host, but if I write

  Directory.CreateDirector("~/poroje/"+s);

访问拒绝错误发生.如果我使用

the access deny error happen. and if i use

  Directory.CreateDirectory(Server.MapPath("~/poroje/"+s));

没有错误,但目录未创建!

there is no error but directory don't create!

这篇关于将数据插入SQL Server不起作用,但是没有错误消息显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆