A
aspnetpal
Hi every1, I have trying to learn datagrid to do simple things but
since day 1 running into problems after problems for really simple
things.
Problem on hand: Display a datagrid containing records from the
database & let user to update few fields. Out of those fields one
should be multi-line (so user can scroll up & down).
So for that particular field I've used textbox (since boundcolumn
doesn't have any property like that...).
After running into problems one after another I finally able to figure
it out how to update the database (SQL Server) for that textbox when
user clicks on the update link.
However when I clicked on the update link 2 things are not happening
first the datagrid is not showing up after the page load. User has to
click on a button to see the datagrid again (not a good design...) &
the values (text) is not displaying in that particular column.
When I open up the database I can see the updated text but why I can't
see it in the datagrid?
I'm posting the aspx code & HTML for datagrid.
Thanks in advance for your help.
Sub dgdSearch_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'Read in the values of the updated row
Dim DemoNumber As Integer = dgdSearch.DataKeys(e.Item.ItemIndex)
'Dim strCustomerName As String = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
'Dim strCustomerNumber As String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
Dim strSerialNumber As String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
'Dim strModelNumber As String = CType(e.Item.Cells(4).Controls(0),
TextBox).Text
'Dim strRMANumber As String = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
'Dim strConditionNumber As String = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
'Dim strLocationNumber As String = CType(e.Item.Cells(7).Controls(0),
TextBox).Text
Dim strStatus As String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
'Dim strRevisedDate As Date = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
Dim strNote As String = CType(e.Item.Cells(10).Controls(1),
TextBox).Text
'Construct the SQL statement using Parameters
Dim strSQL As String = "UPDATE [Equip] SET [SerialNumber] =
@SerialNumber, " & _
"[Status] = @Status, [Note] = @Note " & _
"WHERE [DemoNumber] = @DemoNumber"
' Create Instance of Connection and Command Object
'1. Create a connection
Const strConnString As String = ("server=(local);Integrated
Security=SSPI;database=Equipment Log")
Dim objConn As New SqlConnection(strConnString)
objConn.Open()
Dim myCommand As SqlCommand = New SqlCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
' Add Parameters to the SQL query
Dim parameterDemoNumber As SqlParameter = New
SqlParameter("@DemoNumber", SqlDbType.Int, 4)
parameterDemoNumber.Value = DemoNumber
myCommand.Parameters.Add(parameterDemoNumber)
Dim parameterSerialNumber As SqlParameter = New
SqlParameter("@SerialNumber", SqlDbType.VarChar, 10)
parameterSerialNumber.Value = strSerialNumber
myCommand.Parameters.Add(parameterSerialNumber)
Dim parameterStatus As SqlParameter = New SqlParameter("@Status",
SqlDbType.VarChar, 10)
parameterStatus.Value = strStatus
myCommand.Parameters.Add(parameterStatus)
Dim parameterNote As SqlParameter = New SqlParameter("@Note",
SqlDbType.VarChar, 500)
parameterNote.IsNullable = True
parameterNote.Value = strNote
myCommand.Parameters.Add(parameterNote)
'Execute the UPDATE query
myCommand.ExecuteNonQuery()
objConn.Close()
'Finally, set the EditItemIndex to -1 and rebind the DataGrid
dgdSearch.EditItemIndex = -1
'BindData()
'refresh the datagrid
dgdSearch.DataBind()
End Sub
*******************************************************************
Datagrid's HTML
<asp:datagrid id="dgdSearch"
style="Z-INDEX: 103; LEFT: 48px;
POSITION: absolute; TOP: 504px"
runat="server" Width="272px"
BackColor="White" Height="192px"
CellSpacing="1" AllowSorting="True"
DataMember="Equip" DataKeyField="DemoNumber"
AutoGenerateColumns="False" AllowCustomPaging="True"
AllowPaging="True" GridLines="Horizontal" CellPadding="3"
BorderWidth="1px" BorderStyle="None"
BorderColor="#E7E7FF" ShowFooter="True"
OnCancelCommand="dgdSearch_Cancel" OnEditCommand="dgdSearch_Edit"
OnUpdateCommand="dgdSearch_Update">
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#4A3C8C"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C"
Position="TopAndBottom" BackColor="#E7E7FF"
Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn DataField="DemoNumber" ReadOnly="True"
HeaderText="Demo Number"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerName" ReadOnly="True"
HeaderText="Customer Name"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerNumber" ReadOnly="True"
HeaderText="Customer Number"></asp:BoundColumn>
<asp:BoundColumn DataField="SerialNumber" HeaderText="Serial
Number"></asp:BoundColumn>
<asp:BoundColumn DataField="ModelNumber" ReadOnly="True"
HeaderText="Model Number"></asp:BoundColumn>
<asp:BoundColumn DataField="RMANumber" ReadOnly="True" HeaderText="RMA
Number"></asp:BoundColumn>
<asp:BoundColumn DataField="ConditionNumber" ReadOnly="True"
HeaderText="Condition Number"></asp:BoundColumn>
<asp:BoundColumn DataField="LocationNumber" ReadOnly="True"
HeaderText="Location Number"></asp:BoundColumn>
<asp:BoundColumn DataField="Status"
HeaderText="Status"></asp:BoundColumn>
<asp:BoundColumn DataField="RevisedDate" ReadOnly="True"
HeaderText="Revised Date"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Notes">
<EditItemTemplate>
<asp:TextBox id="txtNote" TextMode="MultiLine" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Note") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
since day 1 running into problems after problems for really simple
things.
Problem on hand: Display a datagrid containing records from the
database & let user to update few fields. Out of those fields one
should be multi-line (so user can scroll up & down).
So for that particular field I've used textbox (since boundcolumn
doesn't have any property like that...).
After running into problems one after another I finally able to figure
it out how to update the database (SQL Server) for that textbox when
user clicks on the update link.
However when I clicked on the update link 2 things are not happening
first the datagrid is not showing up after the page load. User has to
click on a button to see the datagrid again (not a good design...) &
the values (text) is not displaying in that particular column.
When I open up the database I can see the updated text but why I can't
see it in the datagrid?
I'm posting the aspx code & HTML for datagrid.
Thanks in advance for your help.
Sub dgdSearch_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'Read in the values of the updated row
Dim DemoNumber As Integer = dgdSearch.DataKeys(e.Item.ItemIndex)
'Dim strCustomerName As String = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
'Dim strCustomerNumber As String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
Dim strSerialNumber As String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
'Dim strModelNumber As String = CType(e.Item.Cells(4).Controls(0),
TextBox).Text
'Dim strRMANumber As String = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
'Dim strConditionNumber As String = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
'Dim strLocationNumber As String = CType(e.Item.Cells(7).Controls(0),
TextBox).Text
Dim strStatus As String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
'Dim strRevisedDate As Date = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
Dim strNote As String = CType(e.Item.Cells(10).Controls(1),
TextBox).Text
'Construct the SQL statement using Parameters
Dim strSQL As String = "UPDATE [Equip] SET [SerialNumber] =
@SerialNumber, " & _
"[Status] = @Status, [Note] = @Note " & _
"WHERE [DemoNumber] = @DemoNumber"
' Create Instance of Connection and Command Object
'1. Create a connection
Const strConnString As String = ("server=(local);Integrated
Security=SSPI;database=Equipment Log")
Dim objConn As New SqlConnection(strConnString)
objConn.Open()
Dim myCommand As SqlCommand = New SqlCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
' Add Parameters to the SQL query
Dim parameterDemoNumber As SqlParameter = New
SqlParameter("@DemoNumber", SqlDbType.Int, 4)
parameterDemoNumber.Value = DemoNumber
myCommand.Parameters.Add(parameterDemoNumber)
Dim parameterSerialNumber As SqlParameter = New
SqlParameter("@SerialNumber", SqlDbType.VarChar, 10)
parameterSerialNumber.Value = strSerialNumber
myCommand.Parameters.Add(parameterSerialNumber)
Dim parameterStatus As SqlParameter = New SqlParameter("@Status",
SqlDbType.VarChar, 10)
parameterStatus.Value = strStatus
myCommand.Parameters.Add(parameterStatus)
Dim parameterNote As SqlParameter = New SqlParameter("@Note",
SqlDbType.VarChar, 500)
parameterNote.IsNullable = True
parameterNote.Value = strNote
myCommand.Parameters.Add(parameterNote)
'Execute the UPDATE query
myCommand.ExecuteNonQuery()
objConn.Close()
'Finally, set the EditItemIndex to -1 and rebind the DataGrid
dgdSearch.EditItemIndex = -1
'BindData()
'refresh the datagrid
dgdSearch.DataBind()
End Sub
*******************************************************************
Datagrid's HTML
<asp:datagrid id="dgdSearch"
style="Z-INDEX: 103; LEFT: 48px;
POSITION: absolute; TOP: 504px"
runat="server" Width="272px"
BackColor="White" Height="192px"
CellSpacing="1" AllowSorting="True"
DataMember="Equip" DataKeyField="DemoNumber"
AutoGenerateColumns="False" AllowCustomPaging="True"
AllowPaging="True" GridLines="Horizontal" CellPadding="3"
BorderWidth="1px" BorderStyle="None"
BorderColor="#E7E7FF" ShowFooter="True"
OnCancelCommand="dgdSearch_Cancel" OnEditCommand="dgdSearch_Edit"
OnUpdateCommand="dgdSearch_Update">
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#4A3C8C"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C"
Position="TopAndBottom" BackColor="#E7E7FF"
Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn DataField="DemoNumber" ReadOnly="True"
HeaderText="Demo Number"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerName" ReadOnly="True"
HeaderText="Customer Name"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerNumber" ReadOnly="True"
HeaderText="Customer Number"></asp:BoundColumn>
<asp:BoundColumn DataField="SerialNumber" HeaderText="Serial
Number"></asp:BoundColumn>
<asp:BoundColumn DataField="ModelNumber" ReadOnly="True"
HeaderText="Model Number"></asp:BoundColumn>
<asp:BoundColumn DataField="RMANumber" ReadOnly="True" HeaderText="RMA
Number"></asp:BoundColumn>
<asp:BoundColumn DataField="ConditionNumber" ReadOnly="True"
HeaderText="Condition Number"></asp:BoundColumn>
<asp:BoundColumn DataField="LocationNumber" ReadOnly="True"
HeaderText="Location Number"></asp:BoundColumn>
<asp:BoundColumn DataField="Status"
HeaderText="Status"></asp:BoundColumn>
<asp:BoundColumn DataField="RevisedDate" ReadOnly="True"
HeaderText="Revised Date"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Notes">
<EditItemTemplate>
<asp:TextBox id="txtNote" TextMode="MultiLine" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Note") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>