ASP.NET C#; Update Database from Comment Box

Discussion in 'General Webmaster Support' started by Volkof, Mar 5, 2014.

  1. Volkof

    Volkof Well-Known Member

    Ratings:
    +31 / 0 / -0
    I have this Comment Box, whereby the Doctor types in some words, press "Update" and the Comment Box will now show what the Doctor typed.
    In addition, the Database should be updated with the Doctor's comment.
    Problem is that the update did not occur. I typed in the Comment, clicked "Update", the text just reverted back to whatever was originally in the textbox, and the Database was never updated.
    Any advise is appreciated. Thank you

    Code:
    <center>
    <asp:SqlDataSource ID="viewPatSqlDataSource" runat="server"
            ConnectionString="<%$ ConnectionStrings:MISConnectionString %>"
            SelectCommand="SELECT p.Name, p.Gender, p.Age, m.RecordID, m.SickDescription FROM Patientinfo AS p INNER JOIN MedicalRecord AS m ON p.userID = m.userID WHERE (m.DepartmentID = @deptIDParam)">
            <SelectParameters>
                <asp:SessionParameter Name="deptIDParam" SessionField="DeptID" Type="Int16"
                    DefaultValue="" />
            </SelectParameters>
        </asp:SqlDataSource>
     
        <asp:SqlDataSource ID="viewMedSqlDataSource" runat="server"
            ConnectionString="<%$ ConnectionStrings:MISConnectionString %>"       
            SelectCommand="SELECT RecordID, DepartmentID, SickDescription, DoctorComment, userID,Image
    FROM MedicalRecord WHERE (RecordID = @recordIDParam)"
            UpdateCommand="UPDATE MedicalRecord SET DoctorComment = @commentParam WHERE (RecordID = @recordIDParam)">
            <SelectParameters>
                <asp:ControlParameter ControlID="viewPatGridView" Name="recordIDParam"
                    PropertyName="SelectedValue" />
            </SelectParameters>
            <UpdateParameters>
                <asp:ControlParameter ControlID="viewPatDetailsView$commentTextBox" PropertyName="Text" Name="commentParam" Type="String" />     
                <asp:ControlParameter ControlID="viewPatGridView" PropertyName="SelectedValue" Name="recordIDParam" />
            </UpdateParameters>
     
        </asp:SqlDataSource>
     
        <asp:GridView ID="viewPatGridView" runat="server"
            AllowPaging="True"
            AllowSorting="True"
            AutoGenerateColumns="False"
            DataSourceID="viewPatSqlDataSource"
            EnableModelValidation="True"
            DataKeyNames="RecordID"
            Width="500px">
            <Columns>
                <asp:BoundField DataField="RecordID" HeaderText="RecordID"
                    SortExpression="RecordID" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Gender" HeaderText="Gender"
                    SortExpression="Gender" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="SickDescription" HeaderText="SickDescription"
                    SortExpression="SickDescription" />
                <asp:CommandField ShowSelectButton="true" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:DetailsView ID="viewPatDetailsView" runat="server" 
            AutoGenerateRows="False"
            DataSourceID="viewMedSqlDataSource"
            EnableModelValidation="True" 
            DefaultMode="Edit"
            DataKeyNames="RecordID"
            Width="500px" Height="10px">
            <Fields>
                <asp:BoundField DataField="RecordID" HeaderText="RecordID"
                    InsertVisible="False" ReadOnly="True" SortExpression="RecordID" />
                <asp:TemplateField HeaderText="Sick Description" HeaderStyle-Width="150px"  SortExpression="SickDescription">
                    <ItemTemplate>
                        <asp:Label ID="sickDescLabel" runat="server" Text='<%# Bind("SickDescription") %>'></asp:Label>
                    </ItemTemplate>
     
    <HeaderStyle Width="150px"></HeaderStyle>
                </asp:TemplateField>
     
                <asp:TemplateField HeaderText="Image" >                                   
                    <ItemTemplate>                             
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# "HandlerRecord.ashx?ID=" + Eval("RecordID")%>'/>
                    </ItemTemplate>
                </asp:TemplateField>
     
                <asp:TemplateField HeaderText="Comment" HeaderStyle-Width="150px"  SortExpression="DoctorComment">
                    <EditItemTemplate>
                        <asp:TextBox ID="commentTextBox" TextMode="SingleLine" Width="200px" Height="100px" runat="server" Text='<%# Bind("DoctorComment") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <HeaderStyle Width="150px"></HeaderStyle>
                </asp:TemplateField>
     
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="False"
                            CommandName="Edit" Text="Update" />
                    </ItemTemplate>
     
                    <EditItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="True"
                          OnClick ="update"  CommandName="Update" Text="Update" />
                    </EditItemTemplate>
     
                </asp:TemplateField>
            </Fields>
        </asp:DetailsView>
    </center>
    P.S. This program is not written by me, but handed over to me to modify. So I am trying to understand it now in order to debug any errors.
     
  2. rover2341

    rover2341 Is riding a roller coaster...Wee!

    Ratings:
    +114 / 0 / -0
    Edit* I noticed you said you were to edit or fix errors. not try a differnt solution. so my method below likey unhelpful. as its another way to go about it.


    I can tell you how I handle it, but i go about it in a different way. If its not what you wanted, then ignore my answer. First off I am using mySQL so it could be a tad different but should be Very close if you were to do it my way.

    1. I go to nuGet to get the mySQL dlls I need (built into vs).

    2. First I add a Class called Connector, that i can get return no data back, get query data back as DataTable or List<List<string>>
    Code:
        public class Connector
        {
            public static void SendQuery(MySqlCommand cmd)
            {
                cmd.Connection.Open();
                cmd.ExecuteReader();
            }
            public static DataTable QueryDb(MySqlCommand cmd)
            {
                cmd.Connection.Open();
     
                MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                dataAdapter.Fill(dt);
     
                return dt;
     
            }
            public static List<List<string>> QueryDBGetList(MySqlCommand cmd)
            {
                DataTable dt = QueryDb(cmd);
     
                List<List<string>> theRows = new List<List<string>>();
                foreach (DataRow dataRow in dt.Rows)
                {
                    List<string> row = new List<string>();
                    foreach (var v in dataRow.ItemArray)
                    {
                        row.Add(v.ToString());
     
                        if (row.Last() == "")
                        {
                            row.Remove(row.Last());
                            row.Add("null");
                        }
                        row.Add(v.ToString());
                    }
                    theRows.Add(row);
                }
                return theRows;
            }
     
    public static MySqlConnection SqlConnection()
            {
                string connstring = "";
                string dataBase = "";
                string dataSource = ";
                string userID = "";
                string password = "";
                connstring = "Data source=" + dataSource + ";Database=" + dataBase + ";UserID= " + userID + ";Password=" + password;
     
                MySqlConnection connection = new MySqlConnection(connstring);
     
                return connection;
            }
        }
    3. Then In code, on the c# prehaps you use vb. I use this..
    Code:
                MySqlCommand command = new MySqlCommand("select * from user", Connector.SqlConnection());
                var resultRows = Connector.QueryDB(command);
     
     
    

    4. If I want it to have a site with ajax. I create a ashx page,
    //I just pulled some code from one of my apis.
    Code:
       
     
    context.Response.ContentType = "text/plain";
                Connector connector = new Connector();
                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
     
                MySqlCommand command = new MySqlCommand("select * from user", Connector.SqlConnection());
    //When it returns as a list, you can serialize it to json and return it back as a javascrpit object :), if you use a datatable it becomes a mess)
                var resultRows = Connector.QueryDBGetList(command);
     
                context.Response.Write(jsonSerializer.Serialize(resultRows));
     
    
    Then I could send the data though a html request using ajax and get the a return from the database. or if you dont need that you can just say on button click, run the code I had above
     

Share This Page