Char(1) can't be insert from stored procedure called from C#/ASP.n

Discussion in 'ASP .Net' started by JB, Mar 4, 2010.

  1. JB

    JB Guest

    Hello

    I am trying to insert a char(1) field into a table from an ASP.Net/C#
    application.


    At first I was inserting rows into the table but none of the columns that I
    was inserting the rows into were char(1) columns, they were mostly int and
    varchar columns.

    Then I found out that there was one column in the list that was not supposed
    to be an int column but instead it was supposed to be one of the char(1)
    columns into the table.

    So I replaced the int column in the list with the char(1) column and since
    the value of the char(1) column resulted from a CheckBox ('Y' or 'N') in the
    C# ASP.Net program I defined the datatype as:

    char chkYesNo = ' ';

    The way chYesNo gets it's value in the C# ASP.net program is like this:

    if (chkBox1.Checked)
    chkYesNo = 'Y';
    else
    chkYesNo = 'N';

    Now in this Call Behind I call the class in the Data Layer to insert the row
    like this:

    --Call Behind
    InsRow(KeyID, Num1Value, Num2Value, str1Value, str2Value, chkBox2,
    chkYesNo, Date1);


    --Class in Data Layer
    public bool InsertCallIncident(string KeyID, int Num1Value, int Num2Value,
    string str1Value, string str2Value, varchar chkBox2, char chkYesNo,
    DateTime Date1)
    {
    string errors = "";
    SqlParameter[] Params = {new SqlParameter("@ KeyID", KeyID),
    new SqlParameter("@Num1Value", Num1Value),
    new SqlParameter("@Num2Value",
    Num2Value),
    new SqlParameter("@str1Value", str1Value),
    new SqlParameter("@str2Value",str2Value),
    new
    new SqlParameter("@chkBox2", chkBox2),
    SqlParameter("@chkYesNo",chkYesNo),
    new SqlParameter("@Date1",Date1)};

    Lastly this is the stored procedure that inserts the table:

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go

    alter PROCEDURE [dbo].[spInsRow]

    @KeyID varchar(6),
    @Num1Value int,
    @Num2Value int,
    @str1Value varchar(6),
    @str2Value varchar(6),
    @chkBox2 char(1),
    @chkYesNo char(1),
    @Date1 DateTime

    AS


    BEGIN

    If (@KeyID = 'some')
    Begin
    Insert into RTable(keyid, n1, n2,s1, s2,chk1, chk2,someDate)
    Values(@KeyID, @Num1Value, @str1Value, @str2Value, @chkBox2,
    @chkYesNo, @Date1)
    END

    Note: all of these fields accept null values except keyID

    When chk2 was an int field and @chkYesNo was an integer value this worked
    but now that they are char it doesn't work anymore. What is the reason for
    this?

    Jeff


    --
    JB
    JB, Mar 4, 2010
    #1
    1. Advertising

  2. Re: Char(1) can't be insert from stored procedure called fromC#/ASP.n

    On Mar 5, 12:31 am, JB <> wrote:
    > Hello
    >
    > I am trying to insert a char(1) field into a table from an ASP.Net/C#
    > application.
    >
    > At first I was inserting rows into the table but none of the columns that I
    > was inserting the rows into were char(1) columns, they were mostly int and
    > varchar columns.
    >
    > Then I found out that there was one column in the list that was not supposed
    > to be an int column but instead it was supposed to be one of the char(1)
    > columns into the table.
    >
    > So I replaced the int column in the list with the char(1) column and since
    > the value of the char(1) column resulted from a CheckBox ('Y' or 'N') in the
    > C# ASP.Net program I defined the datatype as:
    >
    >         char chkYesNo = ' ';
    >
    > The way chYesNo gets it's value in the C# ASP.net program is like this:
    >
    >             if (chkBox1.Checked)
    >                 chkYesNo = 'Y';
    >             else
    >                 chkYesNo = 'N';
    >
    > Now in this Call Behind I call the class in the Data Layer to insert the row
    > like this:
    >
    > --Call Behind
    > InsRow(KeyID, Num1Value, Num2Value, str1Value, str2Value, chkBox2,  
    > chkYesNo, Date1);
    >
    > --Class in Data Layer
    > public bool InsertCallIncident(string KeyID,  int Num1Value,  int Num2Value,
    >  string str1Value, string str2Value, varchar chkBox2, char chkYesNo,  
    > DateTime Date1)
    >             {
    >                 string errors = "";
    >                 SqlParameter[] Params = {new SqlParameter("@ KeyID",  KeyID),
    >                                     new SqlParameter("@Num1Value", Num1Value),
    >                                     new SqlParameter("@Num2Value",
    > Num2Value),
    >                                     new SqlParameter("@str1Value", str1Value),
    >                                     new SqlParameter("@str2Value",str2Value),
    >                                     new  
    >                                     new SqlParameter("@chkBox2", chkBox2),
    > SqlParameter("@chkYesNo",chkYesNo),
    >                                     new SqlParameter("@Date1",Date1)};
    >
    > Lastly this is the stored procedure that inserts the table:
    >
    > set ANSI_NULLS ON
    > set QUOTED_IDENTIFIER ON
    > go
    >
    > alter PROCEDURE [dbo].[spInsRow]
    >
    > @KeyID           varchar(6),
    > @Num1Value       int,
    > @Num2Value       int,
    > @str1Value       varchar(6),
    > @str2Value       varchar(6),
    > @chkBox2         char(1),
    > @chkYesNo        char(1),
    > @Date1           DateTime
    >
    > AS
    >
    > BEGIN
    >
    >     If (@KeyID = 'some')
    >     Begin
    >         Insert into RTable(keyid, n1, n2,s1, s2,chk1, chk2,someDate)  
    >         Values(@KeyID, @Num1Value, @str1Value, @str2Value, @chkBox2,  
    > @chkYesNo, @Date1)
    >     END
    >
    > Note: all of these fields accept null values except keyID
    >
    > When  chk2 was an int field and @chkYesNo was an integer value this worked
    > but now that they are char it doesn't work anymore.  What is the reason for
    > this?
    >
    > Jeff
    >
    > --
    > JB


    What about other values? For instance, you have another char parameter
    "@chkBox2" which was not mentioned. Try to execute your procedure from
    sql

    exec spInsRow(1,2,3,....)

    to see if all other parameters will be accepted
    Alexey Smirnov, Mar 5, 2010
    #2
    1. Advertising

  3. JB wrote:
    > Hello
    >
    > I am trying to insert a char(1) field into a table from an ASP.Net/C#
    > application.
    >
    >
    > At first I was inserting rows into the table but none of the columns that I
    > was inserting the rows into were char(1) columns, they were mostly int and
    > varchar columns.
    >
    > Then I found out that there was one column in the list that was not supposed
    > to be an int column but instead it was supposed to be one of the char(1)
    > columns into the table.
    >
    > So I replaced the int column in the list with the char(1) column and since
    > the value of the char(1) column resulted from a CheckBox ('Y' or 'N') in the
    > C# ASP.Net program I defined the datatype as:
    >
    > char chkYesNo = ' ';
    >
    > The way chYesNo gets it's value in the C# ASP.net program is like this:
    >
    > if (chkBox1.Checked)
    > chkYesNo = 'Y';
    > else
    > chkYesNo = 'N';
    >
    > Now in this Call Behind I call the class in the Data Layer to insert the row
    > like this:
    >

    [snip]
    >
    > Note: all of these fields accept null values except keyID
    >
    > When chk2 was an int field and @chkYesNo was an integer value this worked
    > but now that they are char it doesn't work anymore. What is the reason for
    > this?
    >
    > Jeff
    >


    The char(1) data type is not equivalent to a char, it's equivalent to a
    string with one character.

    --
    Göran Andersson
    _____
    http://www.guffa.com
    Göran Andersson, Mar 5, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mona
    Replies:
    1
    Views:
    884
    Tu-Thach
    Jun 27, 2003
  2. Mike P
    Replies:
    0
    Views:
    3,267
    Mike P
    Jun 19, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    990
    Ian Collins
    May 9, 2006
  4. Bill Kellaway

    ASP / Stored Procedure SQL Insert Help

    Bill Kellaway, Oct 10, 2003, in forum: ASP General
    Replies:
    7
    Views:
    200
    Bill Kellaway
    Oct 12, 2003
  5. M Wells
    Replies:
    0
    Views:
    333
    M Wells
    Mar 3, 2004
Loading...

Share This Page