Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Calendar Control with DataGrid? 1

Status
Not open for further replies.

suicidaltendencies

Programmer
Jan 28, 2004
58
US
How do I update the value of the calendar that is in my datagrid? It is bombing out when I try to update the calendar.

Thanks,
Harold


<asp:templatecolumn headerstyle-wrap="false" HeaderText="Date" itemstyle-verticalalign="top">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "AppointmentDate", "{0:MM/dd/yyyy}") %>
</itemtemplate>
<edititemtemplate>
<asp:calendar id="calDate" runat="server" selecteddate = '<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "AppointmentDate")) %>' visibledate='<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "AppointmentDate")) %>'/>
</edititemtemplate>
</asp:templatecolumn>


// Variables
TextBox ctlNotes, ctlTimes;
Calendar ctlCalDate;
DropDownList ctlServiceName;


string strAppointmentID = dgEditAppointments.DataKeys[e.Item.ItemIndex].ToString();

ctlServiceName= (DropDownList)e.Item.FindControl("ddlService");
ctlCalDate = (Calendar)e.Item.FindControl("calDate");
ctlNotes = (TextBox)e.Item.FindControl("txtNotes");
ctlTimes = (TextBox)e.Item.FindControl("txtTimes");

// get the connection
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

// update string
string strMySQL = "Update Appointments Set AppointmentDate = '" + ctlCalDate + "', Notes = '" + ctlNotes.Text + "' WHERE AppointmentID = '" + strAppointmentID + "'";
 
The following is how I do it. Hope this helps.


<asp:TemplateColumn HeaderText="Start Date">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.StartDate") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Calendar id=calStartUpdate runat="server" Font-Size="9px" Font-Names="Arial" Width="66px" Height="72px" SelectedDate='<%# DataBinder.Eval(Container, "DataItem.StartDate") %>' VisibleDate='<%# DataBinder.Eval(Container, "DataItem.StartDate") %>'>
</asp:Calendar>
</EditItemTemplate>
</asp:TemplateColumn>

Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.UpdateCommand
Dim strStart, strID As String
Dim calField As Calendar
Dim txtBox As TextBox
strID = Trim(e.Item.Cells(16).Text)

calField = e.Item.Cells(1).Controls(1)
strStart = calField.SelectedDate

UpdateRecord(strID, strStart)
dg.EditItemIndex = -1
LoadGrid()
End Sub

Private Sub UpdateRecord(ByVal strID As String, ByVal strStart As String)
lblError.Text = ""
Try
Dim sb As SQLBean = New SQLBean
If sb.updateLeaveDay(strID, strStart) < 1 Then
lblError.Text = "Record was not updated."
End If
Catch ex As Exception
lblError.Text = "Record was not updated. Please try again."
End Try
End Sub

Public Function updateLeaveDay(ByVal strID As String, ByVal strStart As String) As Integer
Dim intUpdate As Integer = 0

Try
Dim strSQL As String = "Update TableName Set StartDate_DT = @strStart Where ID = @strID"
conn = New SqlConnection(strConn)
Dim cmd As SqlCommand = New SqlCommand(strSQL, conn)
cmd.Parameters.Add(New SqlParameter("@strID", SqlDbType.VarChar, (20)))
cmd.Parameters("@strID").Value = strID
cmd.Parameters.Add(New SqlParameter("@strStart", SqlDbType.DateTime))
cmd.Parameters("@strStart").Value = strStart

conn.Open()
intUpdate = cmd.ExecuteNonQuery()
Catch ex As Exception
intUpdate = 0
Finally
If Not conn Is Nothing Then
conn.Close()
End If
End Try

Return intUpdate
End Function

Hope everyone is having a great day!

Thanks - Jennifer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top