insert in DataGrid not working

T

tshad

I can't seem to get insert into a DataGrid to work.

I am using an example from the net and made changes to work on my computer.
The grid comes up fine. The edit boxes in the footer are there. But when I
press the "insert" link, it never seems to go to the DataGrid1_ItemCommand
routine.

**************************************************************************************
<%@ Page Language="vb" trace="true" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
Dim connstr As String =
System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_ftsolutions")

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)

If Not Page.IsPostBack Then
BindGrid()
End If
End Sub

Sub BindGrid()
Dim cnn As New SqlConnection(connstr)
Dim da As New SqlDataAdapter("select employeeid,lastname,firstname
from northwind..employees", cnn)
Dim ds As New DataSet()
da.Fill(ds, "employees")
DataGrid1.DataSource = ds
DataGrid1.DataBind()

End Sub

Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
trace.warn("inside selectedIndexChanged")
End Sub

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
trace.warn("inside itemCommand")
If e.CommandName = "Insert" Then
Dim cnn As New SqlConnection(connstr)
Dim t1 As TextBox = e.Item.FindControl("textbox2")
Dim t2 As TextBox = e.Item.FindControl("textbox4")
cnn.Open()
Dim sqlCmd as String = "insert into
northwind..employees(lastname,firstname) values('" & t1.Text & "','" &
t2.Text & "')"
Dim cmd As New SqlCommand(sqlcmd, cnn)
cmd.ExecuteNonQuery()
cnn.Close()
BindGrid()
End If
End Sub
</script>
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
ShowFooter="True" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="Employee ID">
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.employeeid") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="LinkButton1" runat="server"
CommandName="Insert">Insert</asp:LinkButton>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox5 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.employeeid") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.lastname") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="First Name">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.firstname") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>
***********************************************************************************

Am I missing something?

Thanks,

Tom
 
T

tshad

I thought I could fix this by adding the onItemCreated line:

<asp:DataGrid id="DataGrid1"
runat="server"
BorderColor="#CC9966"
BorderStyle="None"
BorderWidth="1px"
BackColor="White"
CellPadding="4"
ShowFooter="True"
onItemCreated="DataGrid1_ItemCommand"
AutoGenerateColumns="False">

and I took out the Private from the sub:

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)

But this gives me an error:
********************************************************************************
Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: BC30408: Method 'Public Sub
DataGrid1_ItemCommand(source As Object, e 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)'.
*****************************************************************************************

What are they talking about "same signature"?

This was the line I saw elsewhere that seemed to work.

Thanks,

Tom
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top