Wrox Programmer Forums
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 15th, 2007, 12:00 PM
Authorized User
 
Join Date: Dec 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need Help With DataGrid

I have been stuck on this for 2 days now and can't figure it out.
I'm using a datagrid to get data from SQL DB which works fine. I added an edit feature but nothing changes when update is clicked. I'm lost can some direct me in the right direction?

void dgrdPatientRecords_UpdateCommand( Object s, DataGridCommandEventArgs e )
{
    String constring;
    SqlParameter parmReturn;
    SqlConnection conEck;

    string patientID = Request.QueryString["id"];

    int intID = Convert.ToInt32(dgrdPatientRecords.DataKeys[e.Item.ItemIndex]);
    String strDayOfWeek = ((TextBox)(e.Item.Cells[3].Controls[1])).Text;
    String strDate = ((TextBox)(e.Item.Cells[4].Controls[1])).Text;
    String strWornDay = ((TextBox)(e.Item.Cells[5].Controls[1])).Text;
    String strWornNight = ((TextBox)(e.Item.Cells[6].Controls[1])).Text;
    String strTotalWorn = ((TextBox)(e.Item.Cells[7].Controls[1])).Text;
    String strPainDay = ((TextBox)(e.Item.Cells.Controls[1])).Text;
    String strPainNight = ((TextBox)(e.Item.Cells[9].Controls[1])).Text;

    int intWornDay = Convert.ToInt32(strWornDay);
    int intWornNight = Convert.ToInt32(strWornNight);
    int intWornTotal = intWornDay + intWornNight;

    string strWornTotal = Convert.ToString(intWornTotal);
    int intDayNum = e.Item.ItemIndex;

    lblPatient.Text = patientID;
    intDayNum = intDayNum + 1;

    string strDayNum = Convert.ToString(intDayNum);
    strSql = "UPDATE [EckStudy].[dbo].[PatientRecord] SET [PatientID]=@PatientID, " + "[DayNumber]=@strDayNum, " + "[DayOfWeek]=@strDayOfWeek, " + "[Date]=@strDate, " + "[WornDay]=@strWornDay, " + "[WornNight]=@strWornNight, " + "[TotalWorn]=@strTotalWorn, " + "[PainDay]=@strPainDay, " + "[PainNight]=@strPainNight Where [ID]=@intID";

    constring = ConfigurationSettings.AppSettings["constring"];
    conEck = new SqlConnection(constring);
    SqlCommand cmdSql = new SqlCommand(strSql, conEck);
    cmdSql.Parameters.Add("@intID", intID);
    cmdSql.Parameters.Add("@PatientID", patientID);
    cmdSql.Parameters.Add("@strDayNum", strDayNum);
    cmdSql.Parameters.Add("@strDayOfWeek", strDayOfWeek);
    cmdSql.Parameters.Add("@strDate", strDate);
    cmdSql.Parameters.Add("@strWornDay", strWornDay);
    cmdSql.Parameters.Add("@strWornNight", strWornNight);
    cmdSql.Parameters.Add("@strTotalWorn", strWornTotal);
    cmdSql.Parameters.Add("@strPainDay", strPainDay);
    cmdSql.Parameters.Add("@strPainNight", strPainNight);

    conEck.Open();
    cmdSql.ExecuteNonQuery();
    conEck.Close();
    dgrdPatientRecords.EditItemIndex = -1;
    BindDataGrid();
}

<asp:DataGrid
  ID="dgrdPatientRecords"
  OnEditCommand="dgrdPatientRecords_EditCommand"
  OnUpdateCommand="dgrdPatientRecords_UpdateCommand"
  OnCancelCommand="dgrdPatientRecords_CancelCommand"
  DataKeyField="ID"
  AutoGenerateColumns="False"
  CellPadding="4"
  Runat="Server" Font-Names="Arial" Font-Size="XX-Small" HorizontalAlign="Center" ForeColor="#333333">
<Columns>
    <asp:BoundColumn DataField="ID" HeaderText="ID" Visible="false" />
    <asp:BoundColumn DataField="PatientID" HeaderText="PatientID" Visible="false" />
    <asp:BoundColumn DataField="DayNumber" HeaderText="DayNumber" ReadOnly="true" />

  <asp:templatecolumn HeaderText="Day of Week" HeaderStyle-Width="50">
    <ItemTemplate>
        <asp:label ID="lblDayOfWeek" Text='<%#DataBinder.Eval(Container.DataItem, "DayOfWeek" )%>' runat="server"></asp:label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:textbox ID="txtDayOfWeek" BackColor="#FFFF99" width="30" Font-Names="Arial" Font-Size="XX-Small" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DayOfWeek" )%>' /><br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtDayOfWeek" Display="Dynamic" Text="Enter Day of Week" runat="server" /></center>
    </EditItemTemplate>
  </asp:templatecolumn>

  <asp:templatecolumn HeaderText="Date">
    <ItemTemplate>
        <asp:label Text='<%#DataBinder.Eval(Container.DataItem, "Date" )%>'
            runat="server"></asp:label>
        </ItemTemplate>
        <EditItemTemplate>
            <center><asp:textbox BackColor="#FFFF99" ID="txtDate" width="60" Font-Names="Arial" Font-Size="XX-Small"
                runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Date" )%>' /><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
                ControlToValidate="txtDate"
                   Display="Dynamic"
                   Text="Enter Date"
                   Runat="Server" /></center>
        </EditItemTemplate>
  </asp:templatecolumn>
  <asp:templatecolumn HeaderText="Hours Worn During Day" HeaderStyle-Width="60">
    <ItemTemplate>
        <asp:label Text='<%#DataBinder.Eval(Container.DataItem, "WornDay" )%>'
            runat="server"></asp:label>
        </ItemTemplate>
        <EditItemTemplate>
            <center><asp:textbox BackColor="#FFFF99" ID="txtWornDay" width="30" Font-Names="Arial" Font-Size="XX-Small"
                runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "WornDay" )%>' /><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3"
                ControlToValidate="txtWornDay"
                   Display="Dynamic"
                   Text="Enter Hours Worn During Day"
                   Runat="Server" /></center>
        </EditItemTemplate>
  </asp:templatecolumn>
  <asp:templatecolumn HeaderText="Hours Worn During Night" HeaderStyle-Width="60">
    <ItemTemplate>
        <asp:label Text='<%#DataBinder.Eval(Container.DataItem, "WornNight" )%>'
            runat="server"></asp:label>
        </ItemTemplate>
        <EditItemTemplate>
            <center><asp:textbox BackColor="#FFFF99" ID="txtWornNight" width="30" Font-Names="Arial" Font-Size="XX-Small"
                runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "WornNight" )%>' /><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4"
                ControlToValidate="txtWornNight"
                   Display="Dynamic"
                   Text="Enter Hours Worn During Night"
                   Runat="Server" /></center>
        </EditItemTemplate>
  </asp:templatecolumn>
  <asp:templatecolumn HeaderText="Total Worn" HeaderStyle-Width="60">
    <ItemTemplate>
        <asp:label Text='<%#DataBinder.Eval(Container.DataItem, "TotalWorn" )%>'
            runat="server"></asp:label>
        </ItemTemplate>
        <EditItemTemplate>
            <center><asp:textbox BackColor="#FFFF99" ID="txtTotalWorn" width="30" Font-Names="Arial" Font-Size="XX-Small"
                runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "TotalWorn" )%>' /><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5"
                ControlToValidate="txtTotalWorn"
                   Display="Dynamic"
                   Text="Enter Total Worn"
                   Runat="Server" /></center>
        </EditItemTemplate>
  </asp:templatecolumn>

  <asp:templatecolumn HeaderText="Pain During Day" HeaderStyle-Width="60">
    <ItemTemplate>
        <asp:label Text='<%#DataBinder.Eval(Container.DataItem, "PainDay" )%>'
            runat="server"></asp:label>
        </ItemTemplate>
        <EditItemTemplate>
            <center><asp:textbox BackColor="#FFFF99" ID="txtPainDay" width="30" Font-Names="Arial" Font-Size="XX-Small"
                runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "PainDay" )%>' /><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6"
                ControlToValidate="txtPainDay"
                   Display="Dynamic"
                   Text="Enter Pain During Day"
                   Runat="Server" /></center>
        </EditItemTemplate>
  </asp:templatecolumn>
  <asp:templatecolumn HeaderText="Pain During Night" HeaderStyle-Width="60">
    <ItemTemplate>
        <asp:label Text='<%#DataBinder.Eval(Container.DataItem, "PainNight" )%>'
            runat="server"></asp:label>
        </ItemTemplate>
        <EditItemTemplate>
            <center><asp:textbox BackColor="#FFFF99" ID="txtPainNight" width="30" Font-Names="Arial" Font-Size="XX-Small"
                runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "PainNight" )%>' /><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7"
                ControlToValidate="txtPainNight"
                   Display="Dynamic"
                   Text="Enter Pain During Night"
                   Runat="Server" /></center>
        </EditItemTemplate>
  </asp:templatecolumn>
  <asp:EditCommandColumn
    EditText="Edit"
    UpdateText="Update"
    CancelText="Cancel" ItemStyle-Font-Size="XX-Small" >

    <ItemStyle Wrap="False" />

   </asp:EditCommandColumn>
</Columns>
    <HeaderStyle BackColor="RoyalBlue" HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True" ForeColor="White" />
    <SelectedItemStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <AlternatingItemStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="White" ForeColor="#284775" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#F7F6F3" ForeColor="#333333" />
    <EditItemStyle BackColor="White" BorderStyle="None" Width="0px" BorderWidth="0px" Font-Size="X-Small" />
</asp:DataGrid>

 
Old March 15th, 2007, 05:32 PM
Authorized User
 
Join Date: Mar 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JoinTTT
Default

Why didn't you use Sql Data Source? So your issue will be easy.



 
Old March 15th, 2007, 05:41 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Ok I don't have the time to read through all the code, sorry. Have you placed a hook in your code? Is the update method getting called? Does it produce an error?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 16th, 2007, 11:07 AM
Authorized User
 
Join Date: Dec 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When I click update it goes to the procedure. I placed a response.write to make sure. I even tried to make a grid using controls in VS with update. But the same result whatever is changed doesn't get updated in SQL. I'm stumped.

 
Old March 16th, 2007, 11:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there.. are you sure that your id has the right value...

probably id has 0 and that's why nothing happens....

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
 
Old March 16th, 2007, 11:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I agree with the earlier posts.... You posted this in a ASP.NET 2.0 forum, but the code looks like ASP.NET 1.x If you're really using ASP.NET 2, you're much better off using the built-in data source controls and editing capabilities of the GridView.

That said, are you rebinding your DataGrid on every PostBack? If that's the case, changes from the user are overwritten by the data from the database, giving you the impression that the data isn't updated.

Check if you have something like this:

dgrdPatientRecords.DataSource = Bla Bla Bla;
dgrdPatientRecords.DataBind();

and make sure it's within a !Page.IsPostBack block.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old March 16th, 2007, 01:05 PM
Authorized User
 
Join Date: Dec 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thnaks for the advise everyone. The ID has the correct number. I'll try using the data source controls. Thanks again for the help.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom paging in Datagrid with datagrid page count madhusrp ASP.NET 1.0 and 1.1 Professional 12 June 2nd, 2008 01:15 PM
User COntrol Datagrid inside datagrid rodmcleay ASP.NET 1.0 and 1.1 Professional 3 April 14th, 2007 10:11 AM
Creating DataGrid In Repeater/DataGrid liduwan ASP.NET 1.0 and 1.1 Professional 9 March 6th, 2007 03:31 PM
datagrid ajaidass ADO.NET 2 March 1st, 2007 05:36 PM
Need help regarding datagrid ngsharmila .NET Framework 1.x 1 February 1st, 2006 12:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.