2013-08-20 38 views
1

我有非常简单的页面,只是想知道如何将数据从文本框传递到我的过程。我是那种背后到DB从代码中传递参数的新的,我有一个页面的代码是在C#中存储过程,从文本框中传递参数

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <asp:TextBox ID="LastName" runat="server"></asp:TextBox> 
     <asp:TextBox ID="FirstName" runat="server"></asp:TextBox> 
     <asp:TextBox ID="Phone1" runat="server"></asp:TextBox> 
     <asp:TextBox ID="Phone2" runat="server"></asp:TextBox> 
     <br /> 
     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 
     <br /> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TicketsConnectionString %>" SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource> 

    </div> 
    </form> 
</body> 
</html> 

和ASPX代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

public partial class _Default : System.Web.UI.Page 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    string connectionString = ConfigurationManager.ConnectionStrings["TicketsConnectionString"].ConnectionString; 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     DataSet ds = new DataSet(); 
    SqlConnection con = new SqlConnection(connectionString); 
    SqlCommand cmd = new SqlCommand("InsertIntoEmployee", con); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = LastName.Text; 
    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar); 
    cmd.Parameters.Add("@Phone1", SqlDbType.VarChar); 
    cmd.Parameters.Add("@Phone2", SqlDbType.VarChar); 
    cmd.Parameters["@LastName"].Value = LastName.Text; 
    cmd.Parameters["@FirstName"].Value = FirstName.Text; 
    cmd.Parameters["@Phone1"].Value = Phone1.Text; 
    cmd.Parameters["@Phone2"].Value = Phone2.Text; 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    } 
} 

web.config文件看起来像这样

<?xml version="1.0"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 
<configuration> 
    <connectionStrings> 
    <add name="TicketsConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=&quot;C:\Users\Asya\Documents\Visual Studio 2012\WebSites\WebSite6\App_Code\Tickets.mdf&quot;;Integrated Security=True;Connect Timeout=30" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5"/> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
</configuration> 

当我通过简单的数据到我的prosedure它会抛出错误

An SqlParameter with ParameterName '@LastName' is not contained by this SqlParameterCollection. 

如何解决此问题?因为在数据库端程序完美运作 因为我之前真的很喜欢它。和我以前的固定问题,但在这里,另一个

The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. 

Source Error: 


Line 34:   cmd.Parameters["@Phone2"].Value = Phone2.Text; 
Line 35:   con.Open(); 
Line 36:   cmd.ExecuteNonQuery(); 
Line 37:   con.Close(); 
Line 38: 

过程,我用

procedure InsertIntoEmployee 
@LastName Nvarchar (25) , 
@FirstName Nvarchar (25) , 
@Phone1 Nvarchar (25) , 
@Phone2 Nvarchar (25) 
as 
insert into Employee (LastName , FirstName,Phone1,Phone2)values (@LastName,@FirstName,@Phone1,@Phone2); 

当我像 EXEC InsertIntoEmployee N'петров', N'петр', 99999999,null 它完美而从asp.net侧的投掷错误分贝的侧运行prosedure

+0

显示我们的SQL程序标题,请。只是参数说明的部分。 – OnoSendai

+1

看起来像你不断修复的东西,然后改变问题与更多的问题。如果你需要帮助,你可能应该保持关注并批准正确的答案,然后转移到其他不相关的项目上。另外,你使用的许多变量实现IDisposable,应该被丢弃。在线查看有关如何执行SQL查询的更好示例(即,使用{var con = new SQLConnection ...)。 – Ted

+0

我的问题是如何将参数传递到ASP.Net端的过程和与之相关的错误。在数据库方面,这对我来说真的很容易,但asp.net方面一直困扰我 – Andrey

回答

1

试试这个

protected void Button1_Click(object sender, EventArgs e) 
    { 
     DataSet ds = new DataSet(); 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlCommand cmd = new SqlCommand("InsertIntoEmployee", con); 
     cmd.CommandType = CommandType.StoredProcedure;    
     cmd.Parameters.Add("@LastName", SqlDbType.NVarChar(MAX)).Value = LastName.Text; 
     cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar(MAX)).Value = FirstName.Text; 
      cmd.Parameters.Add("@Phone1", SqlDbType.NVarChar(MAX)).Value = Phone1.Text; 
     cmd.Parameters.Add("@Phone2", SqlDbType.NVarChar(MAX)).Value = Phone2.Text; 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 

     // gvQuarterlyReport.DataBind(); 
    } 
+0

'该字符串未被识别为有效的DateTime。有一个从索引0开始的未知单词。第36行:cmd.ExecuteNonQuery(); – Andrey

+0

我没有在代码中看到日期列?哪里是 ?名字和姓氏是日期时间字段? –

+0

没有所有的字段是Nvarchar。 – Andrey

3

纠正我,如果我错了,但似乎这行

cmd.Parameters.Add("@LastName ", SqlDbType.Date); 

后有一个额外的空间姓氏。应该是

cmd.Parameters.Add("@LastName", SqlDbType.Date); 

取而代之?

2

您需要从“@LastName”删除空格

喜欢的东西

cmd.Parameters.Add("@LastName", SqlDbType.Date); 

但是,这只会在错误转移到鸟巢线@FirstName也没有在参数列表中。

事实上,你有正确的@LastName

所有3后,这些线路会引起问题

cmd.Parameters["@FirstName"].Value = FirstName.Text; 
cmd.Parameters["@Phone1"].Value = Phone1.Text; 
cmd.Parameters["@Phone2"].Value = Phone2.Text 
2

对空间的其他的答案是正确的,当然。请注意,就可以避免这样的问题(或者至少让他们一直错;-)如果指定的参数值向右走,像这样:

cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = LastName.Text; 

而且您的@LastName参数使用SqlDbType.Date。除非你有一个非常奇怪的模式,否则很可能是错误的。因此,我使用上面的SqlDbType.VarChar

+0

+1为真棒“一贯错误”。 =) – OnoSendai

相关问题