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
ataGrid 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
ataGrid>
</form>
</body>
</HTML>
***********************************************************************************
Am I missing something?
Thanks,
Tom
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
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
</form>
</body>
</HTML>
***********************************************************************************
Am I missing something?
Thanks,
Tom