1

I am trying to make zip-code a VarChar in the SQL Database type and have it submit in the database with letters and numbers. When the zip-code has the int SQL Database type the form is submitted into the zip column in the database, but when the value of the zip-code is VarChar 50 the value is not submitted to the new column in the database named zipPostal.

Here is the code in aspx:

    <asp:Label runat="server" ID="zipLabel" AssociatedControlID="zip">Zip/Postal 
     Code</asp:Label>     
    <asp:TextBox ID="zip" name="zip" runat="server"></asp:TextBox>

Here is the snippets of the code in C#:

    /// Form post data from registration form
    string username = Request.Form["UserName"];
    string first = Request.Form["first"];
    string last = Request.Form["last"];
    string zip = Request.Form["zip"];

    sqlCommand.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar, 50,  
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, emails));

    sqlCommand.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar, 15,   
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, username));

    sqlCommand.Parameters.Add(new SqlParameter("@firstName", SqlDbType.VarChar, 20,    
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, first));

    sqlCommand.Parameters.Add(new SqlParameter("@lastName", SqlDbType.VarChar, 20,      
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, last));


sqlCommand.Parameters.Add(new SqlParameter("@zip", SqlDbType.VarChar, 50, 
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, zip));

SQL Command type:

     int chk = sqlCommand.ExecuteNonQuery();

     public string CheckEmail(string Email)
     {

     string returnValue = string.Empty;
     SqlConnection conn = new SqlConnection(strConnection);
     SqlCommand sqlCommand = new SqlCommand

     ("CheckEmailAvailability", conn);
     try
     {
        sqlCommand.CommandType = 

     CommandType.StoredProcedure;
     sqlCommand.Parameters.AddWithValue("@Email", 

     Email.Trim());
        conn.Open();
        returnValue = sqlCommand.ExecuteScalar().ToString();
        conn.Close();
     }
     catch
     {

     }

     finally
     {
        conn.Close();
        sqlCommand.Dispose();
     }

     return returnValue;
     }

////

    public string CheckUsername(string Username)
    {

    string returnValue = string.Empty;
    SqlConnection conn = new SqlConnection(strConnection);
    SqlCommand sqlCommand = new SqlCommand

    ("CheckUsernameAvailability", conn);
    try
    {
        sqlCommand.CommandType = 

    CommandType.StoredProcedure;
        sqlCommand.Parameters.AddWithValue("@UserName", 

    Username.Trim());
        conn.Open();
        returnValue = sqlCommand.ExecuteScalar().ToString();
        conn.Close();
    }
    catch
    {

    }

    finally
    {
        conn.Close();
        sqlCommand.Dispose();
    }

    return returnValue;
    }

    }

//STORED PROCEDURE

   if (username != "")
   {
        string checkUN = CheckUsername(username);

        if (checkUN == "true")
        {
            Label2.Text = "Username already exists.";
            gotoauth = 0;
            return;

        }
    }
    //[ckc] end check username 

    if (password != confirmpwd)
    {
        Label2.Text = "The Password and Confirmation 

   Password must match.";
       gotoauth = 0;
        return;
    }



   //connection with db and call procedure.
    SqlConnection conn = new SqlConnection(strConnection);
    SqlCommand sqlCommand = new SqlCommand

    ("customers_insert", conn);
    sqlCommand.CommandType = CommandType.StoredProcedure;
    conn.Open();


    try
    {


    if (gotoauth == 0) 
     {}


    else
        {



var customerGateway = new 

    CustomerGateway(_loginApikey, _TransactionKey, 

    ServiceMode.Live);
    var NewCustomer = customerGateway.CreateCustomer

    (emails, "Live User Account");
    var datetime = DateTime.Now.ToString(); 

string proid = NewCustomer.ProfileID;
    Session["Customerproid"] = proid;

/// SQL Procedure

    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[customers_insert]
    @email varchar(50) ,
    @username varchar(15) ,
    @firstName varchar(20) ,
    @lastName varchar(20) ,
    @country varchar(50) ,
    @address varchar(100) ,
    @city varchar(100) ,
    @state varchar(50) ,
    @account bigint = null ,
    @profileID bigint = null ,
    @password varchar(20) ,
    @customerNumber int = null ,
    @question varchar(50)  ,
    @answer varchar(50) ,
    @dob varchar(50) ,
    @AccountNumber varchar(50) = null ,
    @PinNumber varchar(50) = null ,
    @PhoneNumber varchar(50) = null, 
    @ApprovalID int,
    @DateTime varchar (50) = null,
    @thoughts varchar (100) = null,
    @zip varchar (50) = null


    AS

    INSERT [dbo].[customers]
    (
    [email],
    [username],
    [firstName],
    [lastName],
    [country],
    [address],
    [city],
    [state],
    [account],
    [profileID],
    [password],
    [customerNumber],
    [question],
    [answer],
    [dob],
    [AccountNumber],
    [PinNumber],
    [PhoneNumber],
    [ApprovalID],
    [DateTime],
    [thoughts],
    [zip]


    )
    VALUES
    (
    @email,
    @username,
    @firstName,
    @lastName,
    @country,
    @address,
    @city,
    @state,
    @account,
    @profileID,
    @password,
    @customerNumber,
    @question,
    @answer,
    @dob,
    @AccountNumber,
    @PinNumber,
    @PhoneNumber,
    @ApprovalID,
    @DateTime,
    @thoughts,
    @zip

    )

In the SQL Database, there is a column for zip (int, not null) and a column for zipPostal (varChar(50), null). I want the zip-code data to be going in the zipPostal column so it accepts letters and numbers.

Any help would be greatly appreciated.

4

2 に答える 2

1

SqlCommandほとんどの場合、列を参照するクエリ (ストアド プロシージャの形式のバックエンド データベース、または a の形式の C# コード) がありzipます。適切なフィールドを参照するには、そのクエリ/手順を変更する必要があります。値を に挿入する必要があるとコードで指定されている場合、 に挿入されるzipことはありませんzipPostal

編集: ストアド プロシージャを確認できるようになったので、これが問題であることが確認されました。修正方法は次のとおりです。

ALTER PROCEDURE [dbo].[customers_insert]
@email varchar(50) ,
@username varchar(15) ,
@firstName varchar(20) ,
@lastName varchar(20) ,
@country varchar(50) ,
@address varchar(100) ,
@city varchar(100) ,
@state varchar(50) ,
@account bigint = null ,
@profileID bigint = null ,
@password varchar(20) ,
@customerNumber int = null ,
@question varchar(50)  ,
@answer varchar(50) ,
@dob varchar(50) ,
@AccountNumber varchar(50) = null ,
@PinNumber varchar(50) = null ,
@PhoneNumber varchar(50) = null, 
@ApprovalID int,
@DateTime varchar (50) = null,
@thoughts varchar (100) = null,
@zip varchar (50) = null


AS

INSERT [dbo].[customers]
(
[email],
[username],
[firstName],
[lastName],
[country],
[address],
[city],
[state],
[account],
[profileID],
[password],
[customerNumber],
[question],
[answer],
[dob],
[AccountNumber],
[PinNumber],
[PhoneNumber],
[ApprovalID],
[DateTime],
[thoughts],
[zipPostal] /*Here's what I changed*/


)
VALUES
(
@email,
@username,
@firstName,
@lastName,
@country,
@address,
@city,
@state,
@account,
@profileID,
@password,
@customerNumber,
@question,
@answer,
@dob,
@AccountNumber,
@PinNumber,
@PhoneNumber,
@ApprovalID,
@DateTime,
@thoughts,
@zip

)

また、これをより複雑な方法で編集して、郵便番号をZipまたはzipPostalその値に応じて配置することもできますが、すべてを に配置する必要があると思いますzipPostal。Galactic Cowboy が以下のコメントで書いているように、すべてのデータを 1 か所に保管し、Zip Code データは常に数値ではなく文字列として保存する必要があります。

于 2013-03-11T16:50:23.283 に答える
0

関連する SQL コードを投稿していません。

「CheckUserNameAvailability」ストアド プロシージャを呼び出すコードと、「CheckEmailAvailability」という名前のストアド プロシージャを呼び出す別のコードを投稿しました。これらのストアド プロシージャはいずれも、郵便番号を設定しようとするコードに関連していないようです。

@Zip をパラメーターとして受け取る別のストアド プロシージャを呼び出していると思われます。そのストアド プロシージャを編集して、正しい列を更新する必要があります。

アップデート:

変化する

INSERT [dbo].[customers]
(
[email],
[username],
[firstName],
[lastName],
[country],
[address],
[city],
[state],
[account],
[profileID],
[password],
[customerNumber],
[question],
[answer],
[dob],
[AccountNumber],
[PinNumber],
[PhoneNumber],
[ApprovalID],
[DateTime],
[thoughts],
[zip]


)
VALUES
(
@email,
@username,
@firstName,
@lastName,
@country,
@address,
@city,
@state,
@account,
@profileID,
@password,
@customerNumber,
@question,
@answer,
@dob,
@AccountNumber,
@PinNumber,
@PhoneNumber,
@ApprovalID,
@DateTime,
@thoughts,
@zip

)

INSERT [dbo].[customers]
(
[email],
[username],
[firstName],
[lastName],
[country],
[address],
[city],
[state],
[account],
[profileID],
[password],
[customerNumber],
[question],
[answer],
[dob],
[AccountNumber],
[PinNumber],
[PhoneNumber],
[ApprovalID],
[DateTime],
[thoughts],
[zipPostal]


)
VALUES
(
@email,
@username,
@firstName,
@lastName,
@country,
@address,
@city,
@state,
@account,
@profileID,
@password,
@customerNumber,
@question,
@answer,
@dob,
@AccountNumber,
@PinNumber,
@PhoneNumber,
@ApprovalID,
@DateTime,
@thoughts,
@zip

)
于 2013-03-11T17:29:15.657 に答える