Solved

Data not updating

Posted on 2011-02-18
2
197 Views
Last Modified: 2012-08-13
I don't understand why my simple form is not updating my table.  The new values are not going into the db, the old values are.  it has something to do with the page_load event, but i can't figure it out.  When i comment out the dataview loading into the text boxes it will save the new elements.  

btw - the drop down list is updating just fine.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class LeaveRequests_NewUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // force the usp_CheckNamedUsers stored procedure to fire
        DataView dvw_DataView = (DataView)usp_CheckNamedUsers.Select(DataSourceSelectArguments.Empty);

       // fill in the user information
        DataView dvw_NamedUser_Current = (DataView)usp_NamedUsers_Current.Select(DataSourceSelectArguments.Empty);
        foreach (DataRowView drv_DataRowView in dvw_NamedUser_Current)
        {
            
            txt_NewUserFullName.Text = drv_DataRowView["vch_UserName"].ToString();
            txt_NewUserUthscsaID.Text = drv_DataRowView["vch_SecondaryUserID"].ToString();
            txt_NewUserPhone.Text = drv_DataRowView["vch_Phone"].ToString();
            txt_EmailAddress.Text = drv_DataRowView["vch_UserEmail"].ToString();
            
        }
    }


    public void usp_CheckNamedUser_Selected(Object sender, SqlDataSourceStatusEventArgs e)
    {
        // grab our return value from the stored procedure
        string str_Test = e.Command.Parameters["@bit_ReturnValue"].Value.ToString();


        // if this user hasn't confirmed their information
        if (str_Test != "False")
        {            
            btn_AddNewUser.Text = "Submit Changes";           
        }

    }

    public void btn_AddNewUser_OnClick(object sender, EventArgs e)
    {
            //Add field validation script here
     
            // call our sql objects to perform the insert of the new request
            // variable declaration
            string vch_ConnectionString = "";                           // load the connection string from web.config
            SqlConnection ptr_SQLConnection;                            // holds the actual SQL Server connection object
            SqlDataAdapter ptr_SQLDataAdapter;                          // holds the SQL Server execution objects
            System.Data.DataSet ptr_SQLDataSet;                         // holds data sets returned back from SQL Server
            System.Configuration.Configuration ptr_WebConfiguration;    //holds our web.config configuration block

            // load our connection string from the web.config file
            vch_ConnectionString = "Data Source=pr2k3ca004;Initial Catalog=udb_LeaveRequests;Integrated Security=True";

            // create a new connection object using our connection sttring
            ptr_SQLConnection = new SqlConnection(vch_ConnectionString);

            // initialize our execution object to use the usp_Request_INSERT stored procedure
            ptr_SQLDataAdapter = new SqlDataAdapter("usp_NamedUsers_Insert", ptr_SQLConnection);

            // tell our execution object that the target is a stored procedure
            ptr_SQLDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

            // add our stored procedure parameters        
            ptr_SQLDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@vch_UserName", SqlDbType.VarChar));
            ptr_SQLDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@vch_Phone", SqlDbType.VarChar));
            ptr_SQLDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@vch_UthscsaID", SqlDbType.VarChar));
            ptr_SQLDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@int_SupervisorID", SqlDbType.Int));
            ptr_SQLDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@vch_UserEmail", SqlDbType.VarChar));

            // assign our stored procedure paramter values
            // varchar values        
            ptr_SQLDataAdapter.SelectCommand.Parameters["@vch_UserName"].Value = txt_NewUserFullName.Text.ToString();
            ptr_SQLDataAdapter.SelectCommand.Parameters["@vch_Phone"].Value = txt_NewUserPhone.Text.ToString();
            ptr_SQLDataAdapter.SelectCommand.Parameters["@vch_UthscsaID"].Value = txt_NewUserUthscsaID.Text.ToString();
            ptr_SQLDataAdapter.SelectCommand.Parameters["@int_SupervisorID"].Value = System.Convert.ToInt32(ddl_NewUserSupervisor.Text, 10);
            ptr_SQLDataAdapter.SelectCommand.Parameters["@vch_UserEmail"].Value = txt_EmailAddress.Text.ToString();

            // create a new data set
            ptr_SQLDataSet = new System.Data.DataSet("usp_NamedUsers_INSERT");

            // fill the data set with the result set from our execution
            ptr_SQLDataAdapter.Fill(ptr_SQLDataSet, "usp_NamedUsers_INSERT");

            // clean up
            ptr_SQLDataAdapter.Dispose();
            ptr_SQLConnection.Close();

            lbl_UserSuccess.Visible = true;
                    
        }
    
    }


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NewUser.aspx.cs" Inherits="LeaveRequests_NewUser" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<!-- My Data Sources -->
    <asp:SqlDataSource ID="usp_CheckNamedUsers" runat="server" 
        ConnectionString="<%$ ConnectionStrings:udb_LeaveRequestsConnectionString %>" 
        DataSourceMode="DataSet" OnSelected="usp_CheckNamedUser_Selected" 
        SelectCommand="usp_CheckNamedUsers" SelectCommandType="StoredProcedure">
            <SelectParameters>
                  <asp:Parameter Direction="Output" Name="bit_ReturnValue" Type="Boolean" />
            </SelectParameters>            
    </asp:SqlDataSource>
<asp:SqlDataSource ID="usp_NamedUsers_Select" runat="server" 
    ConnectionString="<%$ ConnectionStrings:udb_LeaveRequestsConnectionString %>" 
    SelectCommand="usp_NamedUsers_SELECT" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:Parameter DefaultValue="False" Name="bit_CurrentUser" Type="Boolean" />
        </SelectParameters>     
</asp:SqlDataSource>
    <asp:SqlDataSource 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:udb_LeaveRequestsConnectionString %>" 
        SelectCommand="usp_NamedUsers_SELECT" 
        SelectCommandType="StoredProcedure"
        ID="usp_NamedUsers_Current">
        <SelectParameters>
            <asp:Parameter DefaultValue="True" Name="bit_CurrentUser" Type="Boolean" />
        </SelectParameters>
    </asp:SqlDataSource>

<head runat="server">
    <title>Untitled Page</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="shortcut icon" href="/Recruitment/images/favicon.ico" />

    <!-- This controls the background of the sheet -->
    <link href="/LeaveRequests/_style/common_som.css" rel="stylesheet" type="text/css" />

    <!-- This controls the menu formatting -->
    <link href="/LeaveRequests/includes/menus_css/skins/som/primary_vertical.css" rel="stylesheet" type="text/css" />
    <link href="/LeaveRequests/_style/box_styles.css" rel="stylesheet" type="text/css" />

    <!--#include file="/Recruitment/includes/regexChecks.aspx" -->
    <script src="/LeaveRequests/includes/menus_css/js/cssmenus.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
  
        <!-- begin header -->
        <div id="header">
        <div class="logo">
        <a href="/index.aspx">
            <img src="Images/logo_utmsa.jpg" alt="UTMSA LOGO" width="344" height="82" border="0" /> </a>
        </div>
    
      <!-- common secondary menu -->
	    <div class="secnav"></div>
        <!-- tagline -->
	    <div class="tagline"></div>
        <!-- search form -->	
	    <div class="search">
          </div>
      </div>  
        <div id="page">
            <div id="sidebar">
                <div><!--#include file="/LeaveRequests/includes/menu_primary.html" --></div>
	            <noscript><!--#include file="/LeaveRequests/includes/menu_primary.html" --> </noscript>
	      <div style="clear:both;"></div>
	      <!-- end menu -->
            </div>
   
   <div id="content">
    <div class="boxed">
    <!-- Menu Table -->
    <table>
        <td valign="top" class="som_green">
       
        </td>
        <td>
        <table>
        <!-- Begin Data Entry Area -->
         <tr>
         <td>          
                Full Name(First Last)</td>
        <td>                
                <asp:TextBox ID="txt_NewUserFullName" runat="server" Width="260px"></asp:TextBox><br />
                <asp:Label ID="lbl_test" runat="server"></asp:Label></td>
        </tr>   
       <tr>            
        <td>UTHSCSA User ID</td>
        <td><asp:TextBox ID="txt_NewUserUthscsaID" runat="server" Width="260px"></asp:TextBox></td>
        </tr>
        <tr>
        <td>Phone</td>
        <td><asp:TextBox ID="txt_NewUserPhone" runat="server" Width="260px"></asp:TextBox></td></tr>
        <tr>
        <td>Email</td>
        <td><asp:TextBox ID="txt_EmailAddress" runat="server" Width="260px"></asp:TextBox></td>
        </tr>
        <tr>
        <td>Direct Supervisor</td>
        <td>
            <asp:DropDownList ID="ddl_NewUserSupervisor" runat="server" width="267px" 
                DataSourceID="usp_NamedUsers_Select" DataTextField="vch_UserName" 
                DataValueField="int_NamedUserID">
            </asp:DropDownList>
            </td>
        </tr>
        </tr>
        <tr>
        <td colspan="2" align="center">
            <asp:Button ID="btn_AddNewUser" runat="server" Text="Submit" Width="149px"
             OnClick="btn_AddNewUser_OnClick"/>
        </td>
        </tr>
    </table>
    </td>
    </table>
    
    
   </div>
    </div>
    <asp:Label ID="lbl_UserSuccess" runat="server" Text="Your changes have been saved! Click Manage Requests on the menu to begin." Font-Size="Large" Visible="False"></asp:Label>
    
   
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:gdspeare
2 Comments
 
LVL 7

Accepted Solution

by:
dxdinh earned 500 total points
ID: 34928916
check for the IsPostBack from your page load ...something like this


    protected void Page_Load(object sender, EventArgs e)
    {
if (!Page.IsPostBack)
{
        // force the usp_CheckNamedUsers stored procedure to fire
        DataView dvw_DataView = (DataView)usp_CheckNamedUsers.Select(DataSourceSelectArguments.Empty);

       // fill in the user information
        DataView dvw_NamedUser_Current = (DataView)usp_NamedUsers_Current.Select(DataSourceSelectArguments.Empty);
        foreach (DataRowView drv_DataRowView in dvw_NamedUser_Current)
        {
           
            txt_NewUserFullName.Text = drv_DataRowView["vch_UserName"].ToString();
            txt_NewUserUthscsaID.Text = drv_DataRowView["vch_SecondaryUserID"].ToString();
            txt_NewUserPhone.Text = drv_DataRowView["vch_Phone"].ToString();
            txt_EmailAddress.Text = drv_DataRowView["vch_UserEmail"].ToString();
           
        }

} // end if !IsPostBack
    }
0
 

Author Closing Comment

by:gdspeare
ID: 34929915
Perfect.  Thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run time Error 4 47
C# Single Form 8 42
Set value of dynamically created checkboxlist 2 31
Call windows 10 virtual keyboard from windows forms app 2 32
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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