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

  • Thread starter Thread starter JB
  • Start date Start date
J

JB

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
 
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 thatI
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 reasonfor
this?

Jeff

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
 
JB said:
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.
 
Back
Top