473,486 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

insert in DataGrid not working

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
1 1658
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1688
by: 4 | last post by:
Insert/Delete/Modify Statement Shortcuts??? Help!! I am working in C# and running an Oracle 9i server. I was just wonering if there is a shortcut way of updating my tables and not have to...
0
2191
by: Christopher | last post by:
I AM GETTING A SYSTEM.DATA.SQLCLIENT.SQLEXCEPTION ERROR WHEN ATTEMPTING TO INSERT DATA INTO A SINGLE TABLE THROUGH A GRID //If this is due to a spelling error, i will //inflict a ritual...
3
3315
by: mattgcon | last post by:
When I use a stored procedure to insert data into a table and when the Datagrid refreshes two new columns are added. The dataSet is set up with one table with NO columns defined. Please help me on...
10
2936
by: Serdge Kooleman | last post by:
i have a query: select id, name from Dictionary field "id" is guid (so not null, and not identity) i show only "name" in the dataGrid how to set new id when i insert a new record? ...
6
3163
by: Sean | last post by:
HI There, I am making the transition from asp to asp .net, I am currenty writing an application that requires a bulk insert from a webform into SQL server, normally I would just create rows of...
2
1384
by: Mardy | last post by:
Hello I'm working on code to upload an Excel file, read the spreadsheet contents into a datagrid, then ultimately insert each row into a sql server database. Currently I have the file upload...
3
2005
by: pjcraig | last post by:
This is driving me crazy! I have a form that a user will access from another form by selecting the item that they wish to view. When they open the new form, I pass through the id of the item they...
1
3064
by: glenn | last post by:
Hi folks, I am using an Access database, VB.NET and ADO.NET working with a DataGrid control. MY datagrid table has both a date_sent and a date_ans field. When I Insert a record in my SQL...
5
5469
by: Me LK | last post by:
I need to add a line to choose a size into a dropdown . Since I have the dropdown nested inside a datagrid this is not working.When a page displays there are several rows of dropdowns but only the...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.