ASP.Net与SQL Server Express 2008 [英] ASP.Net with SQL Server Express 2008

查看:66
本文介绍了ASP.Net与SQL Server Express 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#& amp;在ASP.Net中创建了一个Web表单。在那里,我创建了一个按钮,用于将数据提交到SQl Server 2008 express db中的表。以下是编码



I have created a web form in ASP.Net using C# & at there I have created a button to submit data to a table in an SQl Server 2008 express db. The following will be the coding

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MyProjects\LeaveMgt\LeaveMgt\App_Data\LeaveMgt.mdf;Integrated Security=True;User Instance=True";
                SqlConnection dbCon = new SqlConnection(connStr);
                SqlCommand comm = new SqlCommand();
                dbCon.Open();
                comm.CommandType = CommandType.Text;
                comm.Connection = dbCon;
                comm.CommandText = ("INSERT INTO LeaveCover VALUES('" + txtEmpNo.Text + "','" + txtEmpName.Text + "','" + txtCoverUp.Text + "','" + txtActOff.Text + "', '" + txtDate.Text + "','" + txtLevAdd.Text + "','" + ddlApprove.Text + "', '" + ddlNor.Text + "','" + ddlNotLev.Text + "')");
                comm.ExecuteNonQuery();
                dbCon.Close();
                lblMessage.Text = "Ok";
            }
            catch {

                lblMessage.Text = "Bad";
            
            }

When I click the submit button it always trig the cath{} but I cannot find the error!
My Database is at the following location and the connection string is as the following.

DB@ <pre lang="c#">D:\MyProjects\LeaveMgt\LeaveMgt\App_Data\LeaveMgt.mdf









ConnectionString是





ConnectionString is

string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MyProjects\LeaveMgt\LeaveMgt\App_Data\LeaveMgt.mdf;Integrated Security=True;User Instance=True";





如果可以,请告诉我错误!



谢谢

Chiranthaka



If you can please show me the error!

Thanks
Chiranthaka

推荐答案

错误是什么?尝试捕获异常,而不仅仅是坏。这毫无意义。此外,您可以单步执行代码并查看它跳转到catch的位置。

实际上,您可能需要指定插入leaevcover(col1,col2等)值的列(0,1,2等)。如果表有一个代理键列或其他列,你没有插入非空的数据。



另外,你不是只是在另一个问这个得到答案就像使用带参数的存储过程一样,这是更好的方法吗?
What is the error? Try catching an exception rather than just "Bad". That is meaningless. Also, you could step through the code and see where it jumps to the catch.
Actually, it kinda looks like you may have to specify the columns like insert into leaevcover (col1, col2, etc) values (0,1,2,etc). If the table has a surrogate key column or other columns you are not inserting data to that are non null.

Also, didn''t you just ask this in another way an get an answer like using a stored procedure with parameters, which is the better way?


它的语句序列没有别的!
Its the sequence of the statements nothing else!


这篇关于ASP.Net与SQL Server Express 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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