Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > hashtable to pass to proc

Reply
Thread Tools

hashtable to pass to proc

 
 
=?Utf-8?B?Q3NoYXJwR3V5?=
Guest
Posts: n/a
 
      02-09-2006
I took over an web app (C#) were the developer put everything in a has table
then called a method to execute a stored procedure, now I'm running into some
issues were if I do an update and a NULL is passed that the field in the db
is left empty and NULL is not entered in. So how can I pass a NULL value in a
hashtable, execute the stored procedure and have NULL entered in the table
instead of a blank field?

here is an example of the code:
the call to the stored procedure and passing parameters:

private voide UpdateCars(string Make, string Model, string Notes)
{
string sql = "spUpdateCars";
System.Collections.HashTable params = new System.Collections.HashTables();
params.add("@make", Make);
params.add("@model", Model);
params.add("@notes", Notes);

this.executeStoredProcedureNonQuery(strSQL, params)

}

now the executeStoredProcedureNonQuery method:
protected int executeStoredPorcedureNonQuery(string sqlCommand ,
System.Collections.Hashtable parameters)
{
int result = 0 ;

SqlConnection conn = getConnection() ;
try
{
SqlCommand cmd = new SqlCommand(sqlCommand, conn) ;
cmd.CommandType = CommandType.StoredProcedure ;
foreach (DictionaryEntry parameterEntry in parameters)
{
cmd.Parameters.Add((string)parameterEntry.Key, parameterEntry.Value) ;

}

cmd.Connection.Open() ;
result = cmd.ExecuteNonQuery() ;
cmd.Connection.Close() ;
}
catch(SqlException e)
{
Console.Write(e.StackTrace) ;
}
finally
{
if (conn != null)
conn.Close() ;
}

return result ;
}

so how can i get it to enter NULL in the field and not leave it blank?



 
Reply With Quote
 
 
 
 
Peter Rilling
Guest
Posts: n/a
 
      02-09-2006
Maybe you could set the HT value to DbNull, or just have a conditional
statement when you build the SqlCommand where if the value is null, then
insert DbNull. Either way you are using DbNull.

"CsharpGuy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I took over an web app (C#) were the developer put everything in a has
>table
> then called a method to execute a stored procedure, now I'm running into
> some
> issues were if I do an update and a NULL is passed that the field in the
> db
> is left empty and NULL is not entered in. So how can I pass a NULL value
> in a
> hashtable, execute the stored procedure and have NULL entered in the table
> instead of a blank field?
>
> here is an example of the code:
> the call to the stored procedure and passing parameters:
>
> private voide UpdateCars(string Make, string Model, string Notes)
> {
> string sql = "spUpdateCars";
> System.Collections.HashTable params = new
> System.Collections.HashTables();
> params.add("@make", Make);
> params.add("@model", Model);
> params.add("@notes", Notes);
>
> this.executeStoredProcedureNonQuery(strSQL, params)
>
> }
>
> now the executeStoredProcedureNonQuery method:
> protected int executeStoredPorcedureNonQuery(string sqlCommand ,
> System.Collections.Hashtable parameters)
> {
> int result = 0 ;
>
> SqlConnection conn = getConnection() ;
> try
> {
> SqlCommand cmd = new SqlCommand(sqlCommand, conn) ;
> cmd.CommandType = CommandType.StoredProcedure ;
> foreach (DictionaryEntry parameterEntry in parameters)
> {
> cmd.Parameters.Add((string)parameterEntry.Key, parameterEntry.Value) ;
>
> }
>
> cmd.Connection.Open() ;
> result = cmd.ExecuteNonQuery() ;
> cmd.Connection.Close() ;
> }
> catch(SqlException e)
> {
> Console.Write(e.StackTrace) ;
> }
> finally
> {
> if (conn != null)
> conn.Close() ;
> }
>
> return result ;
> }
>
> so how can i get it to enter NULL in the field and not leave it blank?
>
>
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q3NoYXJwR3V5?=
Guest
Posts: n/a
 
      02-09-2006
I've tried that and still passing in blank and not NULL to the table.
I did something like;
if(parameterEntry.value == "")
{
parameterEntry.value = System.DBNull.value;
}

and it would not pass NULL to the table, i even checked on the web form
itself,
if (make.text == "")
{
make.text = System.DBNull.value;
}

and still nothing,

"Peter Rilling" wrote:

> Maybe you could set the HT value to DbNull, or just have a conditional
> statement when you build the SqlCommand where if the value is null, then
> insert DbNull. Either way you are using DbNull.
>
> "CsharpGuy" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >I took over an web app (C#) were the developer put everything in a has
> >table
> > then called a method to execute a stored procedure, now I'm running into
> > some
> > issues were if I do an update and a NULL is passed that the field in the
> > db
> > is left empty and NULL is not entered in. So how can I pass a NULL value
> > in a
> > hashtable, execute the stored procedure and have NULL entered in the table
> > instead of a blank field?
> >
> > here is an example of the code:
> > the call to the stored procedure and passing parameters:
> >
> > private voide UpdateCars(string Make, string Model, string Notes)
> > {
> > string sql = "spUpdateCars";
> > System.Collections.HashTable params = new
> > System.Collections.HashTables();
> > params.add("@make", Make);
> > params.add("@model", Model);
> > params.add("@notes", Notes);
> >
> > this.executeStoredProcedureNonQuery(strSQL, params)
> >
> > }
> >
> > now the executeStoredProcedureNonQuery method:
> > protected int executeStoredPorcedureNonQuery(string sqlCommand ,
> > System.Collections.Hashtable parameters)
> > {
> > int result = 0 ;
> >
> > SqlConnection conn = getConnection() ;
> > try
> > {
> > SqlCommand cmd = new SqlCommand(sqlCommand, conn) ;
> > cmd.CommandType = CommandType.StoredProcedure ;
> > foreach (DictionaryEntry parameterEntry in parameters)
> > {
> > cmd.Parameters.Add((string)parameterEntry.Key, parameterEntry.Value) ;
> >
> > }
> >
> > cmd.Connection.Open() ;
> > result = cmd.ExecuteNonQuery() ;
> > cmd.Connection.Close() ;
> > }
> > catch(SqlException e)
> > {
> > Console.Write(e.StackTrace) ;
> > }
> > finally
> > {
> > if (conn != null)
> > conn.Close() ;
> > }
> >
> > return result ;
> > }
> >
> > so how can i get it to enter NULL in the field and not leave it blank?
> >
> >
> >

>
>
>

 
Reply With Quote
 
Peter Rilling
Guest
Posts: n/a
 
      02-09-2006
Have you checked to see if it is a problem with the stored procedure? Maybe
the SP has the code to convert nulls into empty strings. Maybe that field
cannot handle nulls.

"CsharpGuy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I've tried that and still passing in blank and not NULL to the table.
> I did something like;
> if(parameterEntry.value == "")
> {
> parameterEntry.value = System.DBNull.value;
> }
>
> and it would not pass NULL to the table, i even checked on the web form
> itself,
> if (make.text == "")
> {
> make.text = System.DBNull.value;
> }
>
> and still nothing,
>
> "Peter Rilling" wrote:
>
>> Maybe you could set the HT value to DbNull, or just have a conditional
>> statement when you build the SqlCommand where if the value is null, then
>> insert DbNull. Either way you are using DbNull.
>>
>> "CsharpGuy" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >I took over an web app (C#) were the developer put everything in a has
>> >table
>> > then called a method to execute a stored procedure, now I'm running
>> > into
>> > some
>> > issues were if I do an update and a NULL is passed that the field in
>> > the
>> > db
>> > is left empty and NULL is not entered in. So how can I pass a NULL
>> > value
>> > in a
>> > hashtable, execute the stored procedure and have NULL entered in the
>> > table
>> > instead of a blank field?
>> >
>> > here is an example of the code:
>> > the call to the stored procedure and passing parameters:
>> >
>> > private voide UpdateCars(string Make, string Model, string Notes)
>> > {
>> > string sql = "spUpdateCars";
>> > System.Collections.HashTable params = new
>> > System.Collections.HashTables();
>> > params.add("@make", Make);
>> > params.add("@model", Model);
>> > params.add("@notes", Notes);
>> >
>> > this.executeStoredProcedureNonQuery(strSQL, params)
>> >
>> > }
>> >
>> > now the executeStoredProcedureNonQuery method:
>> > protected int executeStoredPorcedureNonQuery(string sqlCommand ,
>> > System.Collections.Hashtable parameters)
>> > {
>> > int result = 0 ;
>> >
>> > SqlConnection conn = getConnection() ;
>> > try
>> > {
>> > SqlCommand cmd = new SqlCommand(sqlCommand, conn) ;
>> > cmd.CommandType = CommandType.StoredProcedure ;
>> > foreach (DictionaryEntry parameterEntry in parameters)
>> > {
>> > cmd.Parameters.Add((string)parameterEntry.Key, parameterEntry.Value) ;
>> >
>> > }
>> >
>> > cmd.Connection.Open() ;
>> > result = cmd.ExecuteNonQuery() ;
>> > cmd.Connection.Close() ;
>> > }
>> > catch(SqlException e)
>> > {
>> > Console.Write(e.StackTrace) ;
>> > }
>> > finally
>> > {
>> > if (conn != null)
>> > conn.Close() ;
>> > }
>> >
>> > return result ;
>> > }
>> >
>> > so how can i get it to enter NULL in the field and not leave it blank?
>> >
>> >
>> >

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?Q3NoYXJwR3V5?=
Guest
Posts: n/a
 
      02-09-2006
The field in the table allows NULLS.

The procs looks like this
create procedure spUpdateCars
{
@salesID int,
@make char(25) = NULL,
@model char(20) = NULL,
@notes varchar(250) = NULL
},
update Cars
set
CarMake = @make,
CarModel = @model,
SaleNotes = @notes
where sales ID = @salesID


is this how it should look to allow NULLS and insert NULL in the table or no?



"Peter Rilling" wrote:

> Have you checked to see if it is a problem with the stored procedure? Maybe
> the SP has the code to convert nulls into empty strings. Maybe that field
> cannot handle nulls.
>
> "CsharpGuy" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I've tried that and still passing in blank and not NULL to the table.
> > I did something like;
> > if(parameterEntry.value == "")
> > {
> > parameterEntry.value = System.DBNull.value;
> > }
> >
> > and it would not pass NULL to the table, i even checked on the web form
> > itself,
> > if (make.text == "")
> > {
> > make.text = System.DBNull.value;
> > }
> >
> > and still nothing,
> >
> > "Peter Rilling" wrote:
> >
> >> Maybe you could set the HT value to DbNull, or just have a conditional
> >> statement when you build the SqlCommand where if the value is null, then
> >> insert DbNull. Either way you are using DbNull.
> >>
> >> "CsharpGuy" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> >I took over an web app (C#) were the developer put everything in a has
> >> >table
> >> > then called a method to execute a stored procedure, now I'm running
> >> > into
> >> > some
> >> > issues were if I do an update and a NULL is passed that the field in
> >> > the
> >> > db
> >> > is left empty and NULL is not entered in. So how can I pass a NULL
> >> > value
> >> > in a
> >> > hashtable, execute the stored procedure and have NULL entered in the
> >> > table
> >> > instead of a blank field?
> >> >
> >> > here is an example of the code:
> >> > the call to the stored procedure and passing parameters:
> >> >
> >> > private voide UpdateCars(string Make, string Model, string Notes)
> >> > {
> >> > string sql = "spUpdateCars";
> >> > System.Collections.HashTable params = new
> >> > System.Collections.HashTables();
> >> > params.add("@make", Make);
> >> > params.add("@model", Model);
> >> > params.add("@notes", Notes);
> >> >
> >> > this.executeStoredProcedureNonQuery(strSQL, params)
> >> >
> >> > }
> >> >
> >> > now the executeStoredProcedureNonQuery method:
> >> > protected int executeStoredPorcedureNonQuery(string sqlCommand ,
> >> > System.Collections.Hashtable parameters)
> >> > {
> >> > int result = 0 ;
> >> >
> >> > SqlConnection conn = getConnection() ;
> >> > try
> >> > {
> >> > SqlCommand cmd = new SqlCommand(sqlCommand, conn) ;
> >> > cmd.CommandType = CommandType.StoredProcedure ;
> >> > foreach (DictionaryEntry parameterEntry in parameters)
> >> > {
> >> > cmd.Parameters.Add((string)parameterEntry.Key, parameterEntry.Value) ;
> >> >
> >> > }
> >> >
> >> > cmd.Connection.Open() ;
> >> > result = cmd.ExecuteNonQuery() ;
> >> > cmd.Connection.Close() ;
> >> > }
> >> > catch(SqlException e)
> >> > {
> >> > Console.Write(e.StackTrace) ;
> >> > }
> >> > finally
> >> > {
> >> > if (conn != null)
> >> > conn.Close() ;
> >> > }
> >> >
> >> > return result ;
> >> > }
> >> >
> >> > so how can i get it to enter NULL in the field and not leave it blank?
> >> >
> >> >
> >> >
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: hashtable or map? (map inserts not behaving as I expect - and I cant find a decent simple example for hashtable) Kai-Uwe Bux C++ 1 12-21-2008 09:25 PM
proc A def/calls proc B: variable scoping rules. NevilleDNZ Python 9 08-16-2006 04:36 AM
Convert VB.NET to TSQL PROC & Reference a Proc from another Proc David Lozzi ASP .Net 3 06-01-2005 06:35 PM
Why no Proc##[]=() ? Why no Proc##replace() ? Jean-Hugues ROBERT Ruby 14 05-05-2004 01:20 PM
What is the diff btwn 'sho proc' and 'sho proc cpu' William J King Cisco 1 12-18-2003 11:50 PM



Advertisments