gerald vince

gerald vince

  • NA
  • 15
  • 7.2k

returning datetime to textbox

Feb 25 2015 4:17 PM
I am trying to get my dropdown list to list the items in my DB based on the users decision. I got it working but it was going to the wrong textboxes and now it says that it cannot convert the datetime to a string (which is how I am pulling them to the textbox. is there a way I can do this while still using a string? or what would the best way to complete this be? all I need is to choose an item in the DDL, then have it spit out the description, startdate, and enddate. so that they can be updated if they need to be. 


HTML

<asp:Panel runat="Server" ID="AnonymousMessagePanel">
        <asp:GridView ID="CompletedProjectsGrid" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="ProjectsClosedList" EnableViewState="False" DataKeyNames="ProjectID">
            <Columns>
                <asp:BoundField DataField="ProjectID" HeaderText="ProjectID" SortExpression="ProjectID" ReadOnly="True" ItemStyle-Width="11%"/>
                <asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName" ItemStyle-Width="11%"/>
                <asp:BoundField DataField="TesterName" HeaderText="TesterName" SortExpression="TesterName" ItemStyle-Width="11%"/>
                <asp:BoundField DataField="ProjectDescription" HeaderText="ProjectDescription" SortExpression="ProjectDescription" ItemStyle-Width="11%"/>
                <asp:BoundField DataField="Platform" HeaderText="Platform" SortExpression="Platform" ItemStyle-Width="11%"/>
                <asp:BoundField DataField="DueDate" HeaderText="DueDate" SortExpression="DueDate" dataformatstring="{0:M/dd/yyyy}" ItemStyle-Width="11%"/>
                <asp:BoundField DataField="DateAssigned" HeaderText="DateAssigned" SortExpression="DateAssigned" dataformatstring="{0:M/dd/yyyy}" ItemStyle-Width="11%"/>
                <asp:BoundField DataField="DocumentName" HeaderText="DocumentName" SortExpression="DocumentName" ItemStyle-Width="11%"/>


            </Columns>
            </asp:GridView>
        <asp:SqlDataSource ID="ProjectsClosedList" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:ProjectsAndTasksTestConnectionString %>" 
                   SelectCommand="SELECT [ProjectID], [ProjectName], [TesterName], [ProjectDescription], [Platform], [DateAssigned], [DueDate], [DateCompleted], [DocumentName] FROM [Projects]">
        </asp:SqlDataSource>








C#

protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected = DDL1.SelectedItem.Value;




            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectsAndTasksTestConnectionString"].ConnectionString);
            using (connection)
            {
                //string selectSQL;
                //selectSQL = "SELECT [ProjectDescription], [DateAssigned], [DueDate] FROM Projects ";
                //selectSQL += "WHERE ProjectName='" + DDL1.SelectedItem.Value + "'";
                //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectsAndTasksTestConnectionString"].ConnectionString);
                //SqlCommand cmd = new SqlCommand(selectSQL, con);
                //SqlDataReader reader;
                SqlCommand command = new SqlCommand("SELECT [ProjectDescription], [DateAssigned], [DueDate] FROM Projects WHERE ProjectName='" + DDL1.SelectedItem.Text + "'", connection);
                command.Parameters.AddWithValue("@DDL1", selected);
                command.CommandType = CommandType.Text;
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                using (reader)
                {
                    if (reader.HasRows)
                    {
                        reader.Read();


                        //add as many as needed to fill textboxes
                        UpdatetxtProjectDesc.Text = reader.GetString(1);
                        UpdatetxtStartDate.Text = reader.GetString(2);
                        UpdatetxtEndDate.Text = reader.GetString(3);
                    }
                    else { }
                }
            }



UPDATE**
Figured it out, enjoy!

UpdatetxtProjectDesc.Text = reader["ProjectDescription"].ToString();
                        UpdatetxtStartDate.Text = ((DateTime)reader["DateAssigned"]).ToString("MM/dd/yyyy");
                        UpdatetxtEndDate.Text = ((DateTime)reader["DueDate"]).ToString("MM/dd/yyyy");