Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please please tell me what the &*$% I'm missing!!!

Status
Not open for further replies.

chromarog

MIS
Mar 15, 2001
61
US
I'm trying to do the right thing by using stored procs but i'm really leaning toward ad-hoc if I can't figure this out. I've been working on this for two days now and I'm getting no where.

In short the page is loaded with ONE line in a datagrid with a button to order. The page load works great, it populates the cells and I'm tickled about that. Now I want to send the data back to the database.

Please help me wrap this up today. I'd like to end my work week on a plus.

ASP Side:
<asp:DataGrid ID="orderdatagrid" runat="server" AutoGenerateColumns="false">
<Columns>

<asp:TemplateColumn HeaderText="Partnumber">
<ItemTemplate>
<asp:Label ID="lblPartNumber" runat="server" text='<%#(databinder.eval(container.dataitem, "partnumber" ).ToString()) %>' />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Actual Count">
<ItemTemplate>
<asp:Label ID="lblActual" runat="server" text='<%#(databinder.eval(container.dataitem, "actual" ).ToString()) %>' />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Order Point">
<ItemTemplate>
<asp:Label ID="lblOrderPoint" runat="server" text='<%#(databinder.eval(container.dataitem, "orderpoint" ).ToString()) %>' />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Order">
<ItemTemplate>
<asp:Button CommandName="Insert" Text="Order Now" runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Order Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Text='<%#(DataBinder.Eval(Container.DataItem, "actual" ).ToString())%>' />
</ItemTemplate>
</asp:TemplateColumn>

</Columns>

</asp:DataGrid>

VB Side: (minus the page load)
Sub Insert_Record(ByVal src As Object, ByVal args As DataGridCommandEventArgs)

KBCS.Open()


Dim PNOrder As Label = args.Item.FindControl("lblPartNumber")
Dim UName As String = Environment.UserName
Dim OrdQty As TextBox = args.Item.FindControl("txtQty")
Dim datenow As Date = Now

If args.CommandName = "Insert" Then

Dim SqlCmd As New SqlCommand("SPVSKB_ORDERNOW", KBCS)
SqlCmd.CommandType = CommandType.StoredProcedure

SqlCmd.Parameters.Add("@PartNumber", SqlDbType.VarChar).Value = PNOrder

SqlCmd.Parameters.Add("@Uname", SqlDbType.VarChar).Value = UName

SqlCmd.Parameters.Add("@QtyOrdered", SqlDbType.Int).Value = OrdQty

SqlCmd.Parameters.Add("@datenow", SqlDbType.DateTime).Value = datenow

SqlCmd.ExecuteNonQuery()

End If


KBCS.Close()


End Sub

 
a couple things
1. are you getting an exception, or just nothing?
2. if there is an exception you must supply the details.
3. if not, is the grid's row command wired to the event?
4. does the button have it's command name set to "Insert"?

you should also wrap the connection and command object in a using statement to guarantee these objects are disposed of in the event of an exception. as it stands now, when an exception is thrown the connection will remain open and the objects will not be disposed. for more information check out the using keyword.

what do you mean by
I'm trying to do the right thing by using stored procs
? stored procs are right or wrong. ORM frameworks don't use stored procs and are just as efficient.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I'm not getting an exception. I'm just getting nothing. I've tried following the code with breakpoints in visual studio 2005 but it's not showing me anything, no declarations or anything. I'm leaning towards the button functionality just flat failing but I can't see it. I know the stored proc works, I can manually fire it off and provide params and it does work. The button's commandname is "Insert".

I'm not sure what you mean by "is the grid's row command wired to the event?"
 
Sub Insert_Record will never be fired, like Jason said, you have to use the Grids events. You need to use the GridView_RowCommand event(I think that is it) and then check for the Commandname = "Insert
 
the name of the handler doesn't matter only that the handler's signature matches the event delegate and it's wired up.

you can do this in markup something like
<asp:gridview ... OnRowCommand="insert_row" >
or you can do this in the code behind
Code:
protected void OnInit(EventArgs e)
{
   base.OnInit(e);
   mygridview.RowCommand += insert_row;
}

protected void Dispose()
{
   mygridview.RowCommand- = insert_row;
   base.Dispose();
}

protected void insert_row(object sender, GridViewRowCommandEventArgs e)
{
   ...logic goes here.
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I'm still pretty new to this and I'm having a heck of a time finding the examples and I've never messed with C#, can you convert that to VB for me or send me to a good site that will help me with that? I don't have a problem with learning that.
 
it's good to know mutliple languages :) and there are c# to vb converters (you may need to fix syntax errors).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
So I'm beginning to seriously overload my gray matter with so many different examples I've googled.

I've converted the datagrid to a gridview becauase that's what all the examples are. Then I've changed the sub to:
Sub GridView_Rowcommand(ByVal sender As Object, ByVal args As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles orderdatagrid.RowCommand

The code appears to be the same in the results. Nothing shows up in the DB. The ONLY way I can get this to act any differently is to change the Button to a LinkButton. Then I get an exception error. The good news about this is that it's referring to the Sub GridView_Ro.... where it's falling apart with a No Default Member found for type 'LinkButton'.

I have a suspicion that if I correct this, I'll still have the same problem. I would just be exstatic if I could at least see some data make it to the database.
 
I'm still getting no where. I've googled anything and everything I can think of. The button does nothing. Can someone tell me what I'm missing in 'wiring up' the button?
 
post your code again, as it should have changed with the recommendations above. post the markup related to the gridview
<asp:Gridview ...>...</asp:Gridview>
and post the code behind. There should only be 1 to 3 members involved: OnInit, OnLoad, RowCommand to insert data.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I've tried to understand your C# examples but I'm not getting it. So if you give me examples I'm begging you put them in VB. I'm really really new to this and I promise to learn C# but the deadline is creaping up on me fast.

If someone were to ask me where this is all wrong, it's in the sub header section of the vb. I've tried so many different kinds of headers my head is spinning. The code below give me:
Compiler Error Message: BC30408: Method 'Public Sub orderdatagrid_ItemDataBound(sender As Object, args As System.Web.UI.WebControls.DataGridCommandEventArgs)' does not have the same signature as delegate 'Delegate Sub DataGridItemEventHandler(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)'.


I've searched for the right signature, the right event handlers and I can't get the right combination of syntax to work for me. I've had this in gridview and datagrid. Either compile errors or just flat nothing. I just want it to fire the stored proc and send the data to the db.

ASP CODE:
<asp:datagrid ID="orderdatagrid" runat="server" AutoGenerateColumns="false" onitemdatabound="orderdatagrid_ItemDataBound" >
<Columns>

<asp:BoundColumn HeaderText="Partnumber" DataField="partnumber" />
<asp:BoundColumn Headertext="Actual Count" DataField="actual" />
<asp:BoundColumn HeaderText="Order Point" DataField="orderpoint" />
<asp:buttoncolumn HeaderText="Order" buttontype="PushButton" commandname="btnOrderClick" Text="Order Now" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Text='<%#(DataBinder.Eval(Container.DataItem, "actual" ).ToString())%>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

</asp:datagrid>

VB CODE:
Sub orderdatagrid_ItemDataBound(ByVal sender As Object, ByVal args As DataGridCommandEventArgs)

KBCS.Open()

Dim PNOrder As String = args.CommandSource("lblPartNumber")
Dim Uname As String = Environment.UserName
Dim OrdQty As String = args.CommandSource("txtQty")
Dim datenow As Date = Now

Dim SqlCmd As New SqlCommand("SPVSKB_ORDERNOW", KBCS)
SqlCmd.CommandType = CommandType.StoredProcedure

SqlCmd.Parameters.Add("@PartNumber", SqlDbType.VarChar).Value = PNOrder

SqlCmd.Parameters.Add("@Uname", SqlDbType.VarChar).Value = Uname

SqlCmd.Parameters.Add("@QtyOrdered", SqlDbType.Int).Value = OrdQty

SqlCmd.Parameters.Add("@datenow", SqlDbType.DateTime).Value = datenow

SqlCmd.ExecuteNonQuery()

KBCS.Close()

End Sub
 
the problem is your sigantures do not match. it tells you that in the error. so to correct this change event wiring.
Code:
<asp:datagrid ID="orderdatagrid" runat="server" AutoGenerateColumns="false" [COLOR=blue]onrowcommand[/color]="orderdatagrid_ItemDataBound" >
[\code]
or something like that. because the signature matches you don't need to change the name of the member(orderdatagrid_ItemDataBound), but it would help so that you don't get confused in the future.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Nothing happens to the data, and onrowcommand is not a valid attribute of datagrid.
 
I've converted the grid to a gridview and made the appropriate changes to the asp but still nothing happens. It's as if the procedure just isn't being called.

 
You will have to trace through the code to make sure the code is being called and it is doing what you expect. Also, you should use Try ... Catch statements around your data access code.
 
If I change the button to a linkbutton, I get an error reporting No default member found for type 'LinkButton'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top