Home All Groups Group Topic Archive Search About

SqlParameters SqlDbType.Char in VB Question

Author
11 Jun 2005 4:40 AM
Patrick Olurotimi Ige
I have a checkbox and i want to input Char "Y" or "N"
to the Table
In C# we could use for example :- ptrTest.Value = chkYN.Checked ? "Y" :
"N";

Whats the equivalent in VB.NET?






*** Sent via Developersdex http://www.developersdex.com ***

Author
11 Jun 2005 6:35 AM
Teemu Keiski
Hi,

IIF function
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctiif.asp

Just be aware that in VB.NET it still evaluates both sides even though the
expression returns only True or False

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Are all your drivers up to date? click for free checkup

Author
11 Jun 2005 7:56 AM
Patrick Olurotimi Ige
Thx Teemu that did the trick but i have another Question.

I have a method below and i'm trying to insert a checkBox with ID
"MessageBank" below but 'm getting

Error:-
Object reference not set to an instance of an object.
on the line "parameterMessageBank.Value = MessageBank.Checked"
The Method is in a Class..
What am i doing wrong?



  Public Sub AddProducts(ByVal cartID As String, ByVal productID As
Integer, ByVal quantity As Integer, ByVal MessageBank As
System.Web.UI.WebControls.CheckBox)
            ' Create Instance of Connection and Command Object
            Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
            Dim myCommand As SqlCommand = New
SqlCommand("CMRC_ShoppingCartAddItem", myConnection)

            ' Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure

            ' Add Parameters to SPROC
            Dim parameterProductID As SqlParameter = New
SqlParameter("@ProductID", SqlDbType.Int, 4)
            parameterProductID.Value = productID
            myCommand.Parameters.Add(parameterProductID)

            Dim parameterCartID As SqlParameter = New
SqlParameter("@CartID", SqlDbType.NVarChar, 50)
            parameterCartID.Value = cartID
            myCommand.Parameters.Add(parameterCartID)

            Dim parameterQuantity As SqlParameter = New
SqlParameter("@Quantity", SqlDbType.Int, 4)
            parameterQuantity.Value = quantity
            myCommand.Parameters.Add(parameterQuantity)

            Dim parameterMessageBank As SqlParameter = New
SqlParameter("@MessageBank", SqlDbType.Bit)
            parameterMessageBank.Value = MessageBank.Checked
            myCommand.Parameters.Add(parameterMessageBank)


            ' Open the connection and execute the Command
            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

        End Sub


*** Sent via Developersdex http://www.developersdex.com ***
Author
12 Jun 2005 7:21 AM
Teemu Keiski
Do you pass the CheckBox instance (MessageBank) to the method where this
method is called? (Besides, wouldn't it be enough to just pass the value as
Boolean instead of entire Control?)

7
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Author
12 Jun 2005 1:35 PM
Patrick Olurotimi Ige
Yeah i could pass it as Boolean but wouldn't i have to have the value of
the checkBox checked?And if i assign CheckBox1.Checked to the value it
says its not a member of Boolean.

Any way let me explain my scenario in details :-
I have CheckBox in my webform with a Datalist like so-

<table width="300" border="0">
<tr>
<td>
<asp:CheckBox ID="MessageBank" Runat="server"></asp:CheckBox>
</td>
</tr>
</table>
<asp:datalist id="MyList" runat="server" RepeatColumns="2">
<ItemTemplate>
<td width="100" valign="middle" align="right">
<a href='ProductDetails.aspx?productID=<%#
DataBinder.Eval(Container.DataItem, "ProductID") %>'>
                        <img src='ProductImages/thumbs/<%#
DataBinder.Eval(Container.DataItem, "ProductImage") %>' width="100"
height="75" border="0">
</a>
</td>
<td width="200" valign="middle">
href='ProductDetails.aspx?productID=<%#
DataBinder.Eval(Container.DataItem, "ProductID") %>'>
<span class="ProductListHead">
<%# DataBinder.Eval(Container.DataItem, "ModelName") %>
</span><br>
</a>
<a href='AddToCart.aspx?productID=<%#
DataBinder.Eval(Container.DataItem, "ProductID") %>'>
<font color="#9D0000"><b>Add To Cart<b></font></span>
</a></td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>

The Datalist displays products that would be added to  a Cart by
clicking the AddToCart.aspx with the ProductID link above.But i want the
user to select the checkbox and would like to pass the value to
AddToCart page.

In AddToCart.aspx codebhind i call a Public Method called AddProduct
that inserts the Products

cart.AddProduct(cartId, CInt(Request.Params("ProductID")), 1,
MessageBank)

Below is the Method in the Class that inserts the products
----------------------------------------------------------
Public Sub AddProduct(ByVal cartID As String, ByVal productID As
Integer, ByVal quantity As Integer, ByVal MessageBank As Boolean)

Dim parameterMessageBank As SqlParameter = New
SqlParameter("@MessageBank", SqlDbType.Bit)
            parameterMessageBank.Value = MessageBank
            myCommand.Parameters.Add(parameterMessageBank)

My Question is how would i pass the selected checkBox(MessageBank) to
the AddProduct and i want to insert 1 or 0 to the MessageBank field so
if checked it inserts 1 and if not 0.

Thanks







*** Sent via Developersdex http://www.developersdex.com ***
Author
13 Jun 2005 7:16 AM
Teemu Keiski
Hello,

I see.

The problem is that you link to another page (you use normal <A> with hrefs)
and that other page has no knowledge about the CheckBox on this, sending
page (the one you click link on). You'd need to change it so that you cause
a postback back to this page (using Button or LinkButton etc), handle that
in DataList's ItemCommand (there you could get data about the checked status
of the CheckBox) and redirect to the other page (, if you need to do it on
another page).


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke





Show quoteHide quote
"Patrick Olurotimi Ige" <naijaco***@hotmail.com> wrote in message
news:OjrIKO1bFHA.3844@tk2msftngp13.phx.gbl...
> Yeah i could pass it as Boolean but wouldn't i have to have the value of
> the checkBox checked?And if i assign CheckBox1.Checked to the value it
> says its not a member of Boolean.
>
> Any way let me explain my scenario in details :-
> I have CheckBox in my webform with a Datalist like so-
>
> <table width="300" border="0">
> <tr>
> <td>
> <asp:CheckBox ID="MessageBank" Runat="server"></asp:CheckBox>
> </td>
> </tr>
> </table>
> <asp:datalist id="MyList" runat="server" RepeatColumns="2">
> <ItemTemplate>
> <td width="100" valign="middle" align="right">
> <a href='ProductDetails.aspx?productID=<%#
> DataBinder.Eval(Container.DataItem, "ProductID") %>'>
> <img src='ProductImages/thumbs/<%#
> DataBinder.Eval(Container.DataItem, "ProductImage") %>' width="100"
> height="75" border="0">
> </a>
> </td>
> <td width="200" valign="middle">
> href='ProductDetails.aspx?productID=<%#
> DataBinder.Eval(Container.DataItem, "ProductID") %>'>
> <span class="ProductListHead">
> <%# DataBinder.Eval(Container.DataItem, "ModelName") %>
> </span><br>
> </a>
> <a href='AddToCart.aspx?productID=<%#
> DataBinder.Eval(Container.DataItem, "ProductID") %>'>
> <font color="#9D0000"><b>Add To Cart<b></font></span>
> </a></td>
> </tr>
> </table>
> </ItemTemplate>
> </asp:datalist>
>
> The Datalist displays products that would be added to  a Cart by
> clicking the AddToCart.aspx with the ProductID link above.But i want the
> user to select the checkbox and would like to pass the value to
> AddToCart page.
>
> In AddToCart.aspx codebhind i call a Public Method called AddProduct
> that inserts the Products
>
> cart.AddProduct(cartId, CInt(Request.Params("ProductID")), 1,
> MessageBank)
>
> Below is the Method in the Class that inserts the products
> ----------------------------------------------------------
> Public Sub AddProduct(ByVal cartID As String, ByVal productID As
> Integer, ByVal quantity As Integer, ByVal MessageBank As Boolean)
>
> Dim parameterMessageBank As SqlParameter = New
> SqlParameter("@MessageBank", SqlDbType.Bit)
>            parameterMessageBank.Value = MessageBank
>            myCommand.Parameters.Add(parameterMessageBank)
>
> My Question is how would i pass the selected checkBox(MessageBank) to
> the AddProduct and i want to insert 1 or 0 to the MessageBank field so
> if checked it inserts 1 and if not 0.
>
> Thanks
>
>
>
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
13 Jun 2005 10:36 AM
Patrick Olurotimi Ige
Thats what i came to find out too but i'm still not getting how to pass
the values corretly..
If i use linkButton or Button how would i use it to cause postback?
Can you show me how i can handle that in the DataList's ItemCommand?
Thanks


*** Sent via Developersdex http://www.developersdex.com ***
Author
13 Jun 2005 1:00 PM
Teemu Keiski
Declare a LinkButton instead of the link (in the <ItemTemplate>) and bind
the ID to the CommandArgument

<asp:LinkButton ID="Link1" runat="server" CommandName="link"
CommandArgument='<%#
DataBinder.Eval(Container.DataItem, "ProductID") %>' />

This will automatically cause DataList's ItemCommand event to be raised
(just wire an event handler to it)

Protected Sub DataList1_ItemCommand(sender As Object, ce As
CommandEventArgs) Handles DataList1.ItemCommand

    If ce.CommandName="link" Then
        'Get cheked status
        Dim checked As Boolean=MessageBan.Checked

        'Get the ID from CommandArgument
        Dim productID As Integer=Cint(ce.CommandArgument)

        'And continue from here, either redirecting to another page with
params in querystring
        ' or do the adding already on this method like on following line
        'cart.AddProduct(cartId, productID, 1,checked)

    End If

End Sub


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke





Show quoteHide quote
"Patrick Olurotimi Ige" <naijaco***@hotmail.com> wrote in message
news:OOcyyOAcFHA.924@TK2MSFTNGP10.phx.gbl...
> Thats what i came to find out too but i'm still not getting how to pass
> the values corretly..
> If i use linkButton or Button how would i use it to cause postback?
> Can you show me how i can handle that in the DataList's ItemCommand?
> Thanks
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
14 Jun 2005 4:54 AM
Patrick Olurotimi Ige
Thx Teemu for the reply.
I ended up passing it in the querystring
But i would change it your approach later.
Patrick


*** Sent via Developersdex http://www.developersdex.com ***
Author
20 Jun 2005 4:24 AM
Patrick Olurotimi Ige
Teemu thx for the reply.
After trying to use what you adviced by using the LinkButton.
The LinkButton is placed in A DataList.
The hyperlink i'm going to use is looking like this:-
NavigateUrl='<%#"AddToProduct.aspx?productID=" &
DataBinder.Eval(Container, "DataItem.ProductID").
How can i pass that to the CommandArgument?

The AddProduct.aspx page is going to add the Params to the Database by
calling the method below:-
cart.AddProduct(cartId, productID, 1,checked)

If i use your approach
    Dim checked As Boolean=MessageBan.Checked
where am i i going to define the CheckBox?


Thanks Patrick



*** Sent via Developersdex http://www.developersdex.com ***
Author
22 Jun 2005 7:27 PM
Teemu Keiski
If you have it on Page at top-level, accessing it that way is just fine
(control on Page and then via its member). If it is contained in the same
Item you could run FindControl (e.Item.FindControl("messageBan") to find it)

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Bookmark and Share