By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

insert in DataGrid not working

P: n/a
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.AppSett ings("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
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"tshad" <ts**********@ftsolutions.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
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.AppSett ings("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

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.