Solved

Problems with my update method...

Posted on 2004-10-11
4
197 Views
Last Modified: 2008-03-10
I have a page that have two tables, one for updating personal details, the other updating chiropractic treatment information... The thing is, I use the same structure on both updates, here are mye files:):

cs.file:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;


namespace Prosjekt1
{
      /// <summary>
      /// Summary description for WebForm3.
      /// </summary>
      public class Pasientdet: System.Web.UI.Page
      {
            
            protected System.Web.UI.WebControls.DataGrid Datagrid1;
            protected System.Web.UI.WebControls.Label txtLabel1;
            protected System.Web.UI.HtmlControls.HtmlForm Form1;
            protected SqlCommand objCmd;
            protected SqlDataReader objRdr;
            protected System.Web.UI.WebControls.TextBox betBox;
            protected System.Web.UI.WebControls.TextBox kommBox;
            protected System.Web.UI.WebControls.Button updateButton1;
            protected System.Web.UI.WebControls.Button delButton1;
            protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.Panel Panel1;
            protected System.Web.UI.WebControls.TextBox datoBox;
            protected System.Web.UI.WebControls.TextBox tidBox;
            protected System.Web.UI.WebControls.TextBox typeBox;
            protected System.Web.UI.WebControls.TextBox navnBox;
            protected System.Web.UI.WebControls.TextBox fodtBox;
            protected System.Web.UI.WebControls.TextBox adrBox;
            protected System.Web.UI.WebControls.TextBox postnrBox;
            protected System.Web.UI.WebControls.TextBox stedBox;
            protected System.Web.UI.WebControls.TextBox tlfBox;
            protected System.Web.UI.WebControls.TextBox mobBox;
            protected System.Web.UI.WebControls.TextBox epostBox;
            protected System.Web.UI.WebControls.Button updateButton;
            protected System.Web.UI.WebControls.Button delButton;
            protected System.Web.UI.WebControls.Label Label2;
            protected SqlConnection objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
      
            
            public void Page_Load(object sender, System.EventArgs e)
            {
                  
                  if (Request.QueryString["t_id"] !=null)
                  {
                        BindData1();
                        BindData2();
                        BindData3();
                  }
                  if (!IsPostBack)
                  {
                        BindData1();
                        BindData2();
                  }
                  
                  
            }
            
            

            public void Datagrid1_Page(Object s, DataGridPageChangedEventArgs e)
            {
                  Datagrid1.CurrentPageIndex = e.NewPageIndex;
                  BindData1();
                  txtLabel1.Text = "";
                                    
                  
            }
            
            public void UpdatePasient(Object s, System.EventArgs e)
            {
                  try
                  {
                        objCmd = new SqlCommand("UpdatePasient", objConn);
                        objCmd.CommandType=CommandType.StoredProcedure;
                        objCmd.Parameters.Add("@p_ID" ,Request.QueryString["p_id"]);
                        objCmd.Parameters.Add("@Navn", navnBox.Text);
                        objCmd.Parameters.Add("@Født", fodtBox.Text);
                        objCmd.Parameters.Add("@Adresse", adrBox.Text);
                        objCmd.Parameters.Add("@Postnr", postnrBox.Text);
                        objCmd.Parameters.Add("@Poststed", stedBox.Text);
                        objCmd.Parameters.Add("@Tlf", tlfBox.Text);
                        objCmd.Parameters.Add("@Mob", mobBox.Text);
                        objCmd.Parameters.Add("@Epost", epostBox.Text);
                        objCmd.Parameters.Add("@Oppdatert", System.DateTime.Now.ToShortDateString());
                        objConn.Open();
                        objCmd.ExecuteNonQuery();
                        Label2.Text = "Oppdateringen var vellykket";
                        objConn.Close();
                        BindData2();
                  }
                  catch
                  {
                        Label2.Text = "En feil oppstod, vennligst prøv igjen";
                  }      
                  
                  
            }

            public void UpdateBehandling(Object s, System.EventArgs e)
            {
                        
                  try
                  {
                        objCmd = new SqlCommand("UpdateBehandling", objConn);
                        objCmd.CommandType=CommandType.StoredProcedure;
                        objCmd.Parameters.Add("@t_id" ,Request.QueryString["t_id"]);
                        objCmd.Parameters.Add("@p_id" ,Request.QueryString["p_id"]);
                        objCmd.Parameters.Add("@Dato", datoBox.Text);
                        objCmd.Parameters.Add("@Tid", tidBox.Text);
                        objCmd.Parameters.Add("@Type", typeBox.Text);
                        objCmd.Parameters.Add("@Betalt", betBox.Text);
                        objCmd.Parameters.Add("@Kommentar", kommBox.Text);
                        objConn.Open();
                        objCmd.ExecuteNonQuery();
                        Label1.Text = "Oppdateringen var vellykket";
                        objConn.Close();
                        BindData3();
                  }
                  catch
                  {
                        Label1.Text = "En feil oppstod, vennligst prøv igjen";
                  }
                        
            }
            

            public void BindData1()
            {
                  
                        
                  string strCommand = ("SELECT * FROM Timebok WHERE p_id= " + Request.QueryString["p_id"]);

                  // create the data set command object and dataset
                  SqlDataAdapter dataAdapter =
                        new SqlDataAdapter(strCommand, objConn);
                  DataSet dataSet = new DataSet();

                  // fill the dataset
                  dataAdapter.Fill(dataSet,"Timebok");
                  SqlCommandBuilder bldr = new SqlCommandBuilder(dataAdapter);

                  // get the table
                  DataTable dataTable = dataSet.Tables[0];
                  Datagrid1.DataSource = dataTable;
                  Datagrid1.DataBind();
            }
            
            public void BindData2()
            {
                        objCmd = new SqlCommand("SELECT * FROM Pasientbok WHERE p_id= " + Request.QueryString["p_id"], objConn);
                        objConn.Open();
                        objRdr = objCmd.ExecuteReader();
                        while (objRdr.Read())
                        {
                              navnBox.Text = (string) objRdr["Navn"];
                              fodtBox.Text = (string) objRdr["Født"];
                              adrBox.Text = (string) objRdr["Adresse"];
                              postnrBox.Text = (string) objRdr["Postnr"];
                              stedBox.Text = (string) objRdr["Poststed"];
                              tlfBox.Text = (string) objRdr["Tlf"];
                              mobBox.Text = (string) objRdr["Mob"];
                              epostBox.Text = (string) objRdr["Epost"];
                              
                        }
                        objRdr.Close();
                        objConn.Close();
                  }

            public void BindData3()
            {
                  objCmd = new SqlCommand("SELECT * FROM Timebok WHERE t_id= " + Request.QueryString["t_id"], objConn);
                  objConn.Open();
                  objRdr = objCmd.ExecuteReader();
                  while (objRdr.Read())
                  {
                        datoBox.Text = (string) objRdr["Dato"];
                        tidBox.Text = (string) objRdr["Tid"];
                        typeBox.Text = (string) objRdr["Type"];
                        betBox.Text = (string) objRdr["Betalt"];
                        kommBox.Text = (string) objRdr["Kommentar"];
                  }
                  objRdr.Close();
                  objConn.Close();
                  Panel1.Visible = true;
                  delButton.Visible = false;
                  updateButton.Visible = false;
            }

            

            

            

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.delButton.Click += new System.EventHandler(this.delButton_Click);
                  this.delButton1.Click += new System.EventHandler(this.delButton1_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void delButton_Click(object sender, System.EventArgs e)
            {
                  try
                  {
                        
                        objCmd = new SqlCommand("DeletePasient", objConn);
                        objCmd.CommandType=CommandType.StoredProcedure;
                        objCmd.Parameters.Add("@p_ID" ,Request.QueryString["p_id"]);
                        objConn.Open();
                        objCmd.ExecuteNonQuery();
                        Label1.Text = "Sletting var vellykket";
                        objConn.Close();
                        Response.Redirect("pasientbok.aspx", true);
                  }
                  catch
                  {
                        Label1.Text = "En feil oppstod, vennligst prøv igjen";
                  }      
            }

            private void delButton1_Click(object sender, System.EventArgs e)
            {
                  try
                  {
                        
                        objCmd = new SqlCommand("DeleteBehandling", objConn);
                        objCmd.CommandType=CommandType.StoredProcedure;
                        objCmd.Parameters.Add("@t_ID" ,Request.QueryString["t_id"]);
                        objConn.Open();
                        objCmd.ExecuteNonQuery();
                        Label1.Text = "Sletting var vellykket";
                        objConn.Close();
                        Panel1.Visible = false;
                        BindData1();
                  }
                  catch
                  {
                        Label1.Text = "En feil oppstod, vennligst prøv igjen";
                  }      
            }
            

            

            

            
      }
}



aspx.file:
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
                                                            <tr>
                                                                  <td class="text2">
                                                                        <div align="center"><br>
                                                                              ::PASIENTBOK ::</div>
                                                                  </td>
                                                            </tr>
                                                      </table>
                                                      <form id="Form1" name="form1" action="" method="post" runat="server">
                                                            <FIELDSET class="fieldset" style="WIDTH: 40.59%; HEIGHT: 356px"><LEGEND class="fieldset">Pasientdetalj</LEGEND>
                                                                  <DIV align="left"></DIV>
                                                                  &nbsp;
                                                                  <TABLE id="Table1" style="WIDTH: 352px; HEIGHT: 207px" cellSpacing="0" cellPadding="2"
                                                                        width="352" align="center" border="0">
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px" width="141">Navn:</TD>
                                                                              <TD width="351"><asp:textbox id="navnBox" runat="server" Width="240px" CssClass="field1" Columns="50"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">Født:</TD>
                                                                              <TD><asp:textbox id="fodtBox" runat="server" Width="80px" CssClass="field1"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">Adresse:</TD>
                                                                              <TD><asp:textbox id="adrBox" runat="server" Width="240px" CssClass="field1"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">Postnr:</TD>
                                                                              <TD><asp:textbox id="postnrBox" runat="server" Width="40px" CssClass="field1"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">Poststed:</TD>
                                                                              <TD><asp:textbox id="stedBox" runat="server" Width="168px" CssClass="field1"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">Tlf:</TD>
                                                                              <TD><asp:textbox id="tlfBox" runat="server" Width="80px" CssClass="field1"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">Mob:</TD>
                                                                              <TD><asp:textbox id="mobBox" runat="server" Width="80px" CssClass="field1"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">Epost:</TD>
                                                                              <TD><asp:textbox id="epostBox" runat="server" Width="176px" CssClass="field1"></asp:textbox></TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" style="WIDTH: 141px">&nbsp;</TD>
                                                                              <TD>&nbsp;</TD>
                                                                        </TR>
                                                                        <TR>
                                                                              <TD class="text1" colSpan="2">
                                                                                    <P><asp:button id="updateButton" onclick="UpdatePasient" runat="server" CssClass="sbmButton" Text="Oppdater Pasient"></asp:button><asp:button id="delButton" runat="server" CssClass="sbmButton" Text="Slett Pasient"></asp:button></P>
                                                                              </TD>
                                                                        </TR>
                                                                  </TABLE>
                                                                  <asp:label id="Label2" runat="server" Width="360px" CssClass="Arial1" Font-Size="10pt" Font-Names="Arial"
                                                                        Font-Bold="True" ForeColor="IndianRed"></asp:label></FIELDSET>
                                                            <asp:panel id="Panel1" Runat="server" Visible="false">
                                                                  <FIELDSET class="fieldset" style="WIDTH: 40.59%; HEIGHT: 356px"><LEGEND class="fieldset">Behandlingsdetalj</LEGEND>
                                                                        <DIV align="left"></DIV>
                                                                        &nbsp;
                                                                        <TABLE id="Table2" style="WIDTH: 352px; HEIGHT: 207px" cellSpacing="0" cellPadding="0"
                                                                              width="400" align="center" border="0">
                                                                              <TR>
                                                                                    <TD class="text1" style="WIDTH: 127px" width="127" height="22">Dato:</TD>
                                                                                    <TD width="157">
                                                                                          <asp:textbox id="datoBox" runat="server" Width="80px" CssClass="field1" ReadOnly="true"></asp:textbox></TD>
                                                                              </TR>
                                                                              <TR>
                                                                                    <TD class="text1" style="WIDTH: 127px" height="22">Tid:</TD>
                                                                                    <TD>
                                                                                          <asp:textbox id="tidBox" runat="server" Width="80px" CssClass="field1" ReadOnly="true"></asp:textbox></TD>
                                                                              </TR>
                                                                              <TR>
                                                                                    <TD class="text1" style="WIDTH: 127px" height="22">Type behandling:</TD>
                                                                                    <TD>
                                                                                          <asp:textbox id="typeBox" runat="server" Width="216px" CssClass="field1"></asp:textbox></TD>
                                                                              </TR>
                                                                              <TR>
                                                                                    <TD class="text1" style="WIDTH: 127px" height="18">Betalt:</TD>
                                                                                    <TD>
                                                                                          <asp:TextBox id="betBox" runat="server" CssClass="field1"></asp:TextBox></TD>
                                                                              </TR>
                                                                              <TR>
                                                                                    <TD class="text1" style="WIDTH: 127px" vAlign="top" height="18">Kommentar:</TD>
                                                                                    <TD>
                                                                                          <asp:TextBox id="kommBox" runat="server" Width="224px" CssClass="field1" Height="95px" TextMode="MultiLine"></asp:TextBox></TD>
                                                                              </TR>
                                                                              <TR>
                                                                                    <TD class="text1" colSpan="2" height="16">
                                                                                          <P>&nbsp;</P>
                                                                                    </TD>
                                                                              </TR>
                                                                              <TR vAlign="top">
                                                                                    <TD class="text1" colSpan="2" height="24">
                                                                                          <asp:Button id="updateButton1" onclick="UpdateBehandling" runat="server" CssClass="sbmButton"
                                                                                                Text="Oppdater Behandling"></asp:Button>
                                                                                          <asp:Button id="delButton1" runat="server" CssClass="sbmButton" Text="Slett Behandling"></asp:Button></TD>
                                                                              </TR>
                                                                        </TABLE>
                                                                        <asp:label id="Label1" runat="server" Width="360px" CssClass="Arial1" Font-Size="10pt" Font-Names="Arial"
                                                                              Font-Bold="True" ForeColor="IndianRed"></asp:label></FIELDSET>
                                                            </asp:panel>
                                                            <FIELDSET class="fieldset" style="WIDTH: 25%"><LEGEND class="fieldset">Behandlingsoversikt</LEGEND>
                                                                  <DIV align="right"><BR>
                                                                        <asp:label id="txtLabel1" runat="server" Width="360px"></asp:label></DIV>
                                                                  <asp:datagrid id="Datagrid1" runat="server" Width="456px" Cellpadding="3" CellSpacing="0" BorderWidth="0"
                                                                        HeaderStyle-BackColor="#006696" AlternatingItemStyle-BackColor="#e0dfe3" HeaderStyle-Font-Bold="true"
                                                                        HeaderStyle-Font-Size="10" AutoGenerateColumns="False" GridLines="None" HeaderStyle-HorizontalAlign="Left"
                                                                        ItemStyle-HorizontalAlign="Left" HeaderStyle-ForeColor="#ffffff" ItemStyle-Font-Bold="true"
                                                                        ItemStyle-Font-Size="10" HorizontalAlign="Center" AllowPaging="True" OnPageIndexChanged="Datagrid1_Page">
                                                                        <AlternatingItemStyle BackColor="#E0DFE3"></AlternatingItemStyle>
                                                                        <ItemStyle Font-Size="10pt" Font-Bold="True" HorizontalAlign="Left"></ItemStyle>
                                                                        <HeaderStyle Font-Size="10pt" Font-Bold="True" HorizontalAlign="Left" ForeColor="White" BackColor="#006696"></HeaderStyle>
                                                                        <Columns>
                                                                              <asp:templatecolumn>
                                                                                    <headertemplate>
                                                                                          Behandling
                                                                                    </headertemplate>
                                                                                    <itemtemplate>
                                                                                          <a href='pasientdet.aspx?p_id=<%#DataBinder.Eval(Container.DataItem, "p_id")%>&t_id=<%#DataBinder.Eval(Container.DataItem, "t_id")%>'>
                                                                                                <%#
                                                                        DataBinder.Eval(Container.DataItem, "Dato")%>
                                                                                          </a>
                                                                                    </itemtemplate>
                                                                              </asp:templatecolumn>
                                                                              <asp:BoundColumn DataField="Type" HeaderText="Type Behandling"></asp:BoundColumn>
                                                                              <asp:BoundColumn DataField="Betalt" HeaderText="Betalt"></asp:BoundColumn>
                                                                        </Columns>
                                                                        <PagerStyle NextPageText="Neste 10 &amp;gt;" Font-Size="10pt" Font-Bold="True" PrevPageText="&amp;lt; Forrige 10"
                                                                              HorizontalAlign="Right" ForeColor="Black"></PagerStyle>
                                                                  </asp:datagrid></FIELDSET>
                                                            &nbsp;
                                                      </form>
                                                      <P>&nbsp;</P>
                                                </td>
                                                <td vAlign="top" width="170"><BR>
                                                      <table class="menutext1" cellSpacing="0" cellPadding="0" width="130" align="center" border="0">
                                                            <tr>
                                                                  <td class="tblhead1">Snarveier</td>
                                                            </tr>
                                                            <tr>
                                                                  <td><A href="timebok.aspx">:: Registrer ny time</A>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td><A href="gjormal.aspx">:: Registrer nytt gjøremål</A></td>
                                                            </tr>
                                                            <tr>
                                                                  <td><A href="pasientbok.aspx">:: Registrer ny pasient</A></td>
                                                            </tr>
                                                            <tr>
                                                                  <td><A href="default.aspx">:: Dagens timebok </A>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td><A href="utestaende.aspx">:: Utestående</A>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>&nbsp;
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>&nbsp;</td>
                                                            </tr>
                                                      </table>
                                                      <div align="center"></div>
                                                </td>
                                          </tr>
                                    </TBODY>
                              </table>
                        </td>
                  </tr>
            </table>
            <script language="javascript">
            // client side confirmation, no server round trip.
            function onclick_function()
            {
            return confirm("Er du helt sikker på at du vil slette pasienten?");
            // if confirmed form is getting submitted for Delete Record Button click
            }

            if( document.getElementById("delButton") ) {
        document.getElementById("delButton").onclick = onclick_function;
            }
            </script>
            <script language="javascript">
            // client side confirmation, no server round trip.
            function onclick_function()
            {
            return confirm("Er du helt sikker på at du vil slette pasienten?");
            // if confirmed form is getting submitted for Delete Record Button click
            }

            if( document.getElementById("delButton1") ) {
        document.getElementById("delButton1").onclick = onclick_function;
            }
            </script>
SPROCS:
CREATE PROCEDURE [dbo].[UpdateBehandling]

@t_id bigint,
@p_id bigint,
@Dato char (11),
@Tid char (5),
@Type char (50),
@Betalt char (3),
@Kommentar text
AS
Update Timebok
SET       
      Tid =@Tid,
      Dato =@Dato,
      Type =@Type,
      Betalt =@Betalt,
      Kommentar =@Kommentar
      WHERE t_id = @t_id AND p_id = @p_id
GO

CREATE PROCEDURE [dbo].[UpdatePasient]


@p_id bigint,
@Navn char(100),
@Født char(40),
@Adresse char(150),
@Postnr char(4),
@Poststed char (50),
@Tlf char (100),
@Mob char (8),
@Epost char (100),
@Oppdatert char (10)
AS
Update Pasientbok
SET       Navn =@Navn,
      Født  =@Født,
      Adresse =@Adresse,
      Postnr =@Postnr,
      Poststed =@Poststed,
      Tlf =@Tlf,
      Mob =@Mob,
      Epost =@Epost,
      Oppdatert =@Oppdatert
      Where p_id = @p_id
GO
0
Comment
Question by:ginola14
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:ginola14
ID: 12279501
The problem is the UpdateBehandling() method, it doesn't update the database, what's wrong? I don't get any error messages either...
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 500 total points
ID: 12279677
Modify your Page_Load like following:

------------------------------------------------------------------------------------------------
public void Page_Load(object sender, System.EventArgs e)
          {
               if (!IsPostBack)
               {
                    if (Request.QueryString["t_id"] !=null)
                    {
                        BindData1();
                        BindData2();
                        BindData3();
                    }

                    BindData1();
                    BindData2();
               }
          }
------------------------------------------------------------------------------------------------

It seems to me that Request.QueryString["t_id"] is not null so even if you Update your datagrid the value gets reset everytime before the event get fired.

-tushar
0
 

Author Comment

by:ginola14
ID: 12280016
that was it.
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12280029
Nice to have you going!

Thanks for the A

-tushar
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question