Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. GridView & SQLdatasourceproblems

GridView & SQLdatasourceproblems

Scheduled Pinned Locked Moved ASP.NET
questioncssannouncement
16 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Ok I see what you are saying.. but won't Convert.ToDateTime insert time to? I just need the Date. Also I do not have any parameters set up in the update. When someone clicks the update in the column (Update) next to the row (this is all built into the gridview) how do I specify what it is updating?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    Well I guess if I still convert the users input (after setting a validation like \d{2}\\d{2}\\d{4}) it will still convert and do the calculation right. Because if you don't specify a time it will input 12:00:00 AM for you. Also here is what I have for the update: UPDATE Clipboard SET County = @County, Type = @Type, Date = @Date WHERE (IncidentNumber = @IncidentNumber)

    K 1 Reply Last reply
    0
    • L Lost User

      Ok I see what you are saying.. but won't Convert.ToDateTime insert time to? I just need the Date. Also I do not have any parameters set up in the update. When someone clicks the update in the column (Update) next to the row (this is all built into the gridview) how do I specify what it is updating?

      K Offline
      K Offline
      keyboard warrior
      wrote on last edited by
      #5

      Well one way to do this is to in the asp:Gridview tag code you can add DataKeyNames="[id column name here]" <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="lvl1_id" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None"> This will make whatever you put in that for a column the way gridview identify records. So if you're records have an ID column, it should be that. This is probably the method I would use. AND make sure your datasource has an update parameter set: <UpdateParameters> <asp:Parameter Name="lvl1_name" Type="String" /> <asp:Parameter Name="lvl1_id" Type="Int32" /> </UpdateParameters> This is part of the DataSOURCE code. See the update parameters....in the SQLDataSource, this is where you can set it up. what this is saying it take the text box for lvl1_name and the lvl1_id for that record and perform a sql update. So if it's not updating, I would say it's because your datasource code in the .aspx is missing those update parameters. I will include a datasource next. <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:blahblah %>" DeleteCommand="UPDATE [bmj_level1] SET [enabled] = 0 WHERE [lvl1_id] = @lvl1_id" InsertCommand="INSERT INTO [bmj_level1] ([lvl1_name]) VALUES (@lvl1_name)" SelectCommand="SELECT [lvl1_id], [lvl1_name] FROM [bmj_level1] WHERE [enabled] = 1 ORDER BY [lvl1_name]" UpdateCommand="UPDATE [bmj_level1] SET [lvl1_name] = @lvl1_name WHERE [lvl1_id] = @lvl1_id"> <DeleteParameters> <asp:Parameter Name="lvl1_id" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="lvl1_name" Type="String" /> <asp:Parameter Name="lvl1_id" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="lvl1_name" Type="String" /> </InsertParameters> </asp:SqlDataSource>

      ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

      L 1 Reply Last reply
      0
      • L Lost User

        Well I guess if I still convert the users input (after setting a validation like \d{2}\\d{2}\\d{4}) it will still convert and do the calculation right. Because if you don't specify a time it will input 12:00:00 AM for you. Also here is what I have for the update: UPDATE Clipboard SET County = @County, Type = @Type, Date = @Date WHERE (IncidentNumber = @IncidentNumber)

        K Offline
        K Offline
        keyboard warrior
        wrote on last edited by
        #6

        so in your datasource you need to specify under the "Update Parameters" section each one of those <UpdateParameters> <asp:Parameter Name="county" Type="String" /> <asp:Parameter Name="type" Type="Int32" /> <asp:Parameter Name="Date" Type="DateTime" /> </UpdateParameters> make sure the parameter datatypes match the sql data types.

        ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

        1 Reply Last reply
        0
        • K keyboard warrior

          Well one way to do this is to in the asp:Gridview tag code you can add DataKeyNames="[id column name here]" <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="lvl1_id" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None"> This will make whatever you put in that for a column the way gridview identify records. So if you're records have an ID column, it should be that. This is probably the method I would use. AND make sure your datasource has an update parameter set: <UpdateParameters> <asp:Parameter Name="lvl1_name" Type="String" /> <asp:Parameter Name="lvl1_id" Type="Int32" /> </UpdateParameters> This is part of the DataSOURCE code. See the update parameters....in the SQLDataSource, this is where you can set it up. what this is saying it take the text box for lvl1_name and the lvl1_id for that record and perform a sql update. So if it's not updating, I would say it's because your datasource code in the .aspx is missing those update parameters. I will include a datasource next. <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:blahblah %>" DeleteCommand="UPDATE [bmj_level1] SET [enabled] = 0 WHERE [lvl1_id] = @lvl1_id" InsertCommand="INSERT INTO [bmj_level1] ([lvl1_name]) VALUES (@lvl1_name)" SelectCommand="SELECT [lvl1_id], [lvl1_name] FROM [bmj_level1] WHERE [enabled] = 1 ORDER BY [lvl1_name]" UpdateCommand="UPDATE [bmj_level1] SET [lvl1_name] = @lvl1_name WHERE [lvl1_id] = @lvl1_id"> <DeleteParameters> <asp:Parameter Name="lvl1_id" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="lvl1_name" Type="String" /> <asp:Parameter Name="lvl1_id" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="lvl1_name" Type="String" /> </InsertParameters> </asp:SqlDataSource>

          ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #7

          Ok I am sort of following you.. Here is my SQLDataSource Code:

          "
          DeleteCommand="DELETE FROM [Clipboard] WHERE [IncidentNumber] = @IncidentNumber"
          InsertCommand="INSERT INTO [Clipboard] ([County], [Type], [Date]) VALUES (@County, @Type, @Date)"
          SelectCommand="SELECT * FROM [Clipboard]"
          UpdateCommand="UPDATE Clipboard SET County = @County, Type = @Type, Date = @Date WHERE (IncidentNumber = @IncidentNumber)";

          DELETE and UPDATE do not work.... uhmm

          modified on Tuesday, May 20, 2008 11:47 PM

          K 1 Reply Last reply
          0
          • L Lost User

            Ok I am sort of following you.. Here is my SQLDataSource Code:

            "
            DeleteCommand="DELETE FROM [Clipboard] WHERE [IncidentNumber] = @IncidentNumber"
            InsertCommand="INSERT INTO [Clipboard] ([County], [Type], [Date]) VALUES (@County, @Type, @Date)"
            SelectCommand="SELECT * FROM [Clipboard]"
            UpdateCommand="UPDATE Clipboard SET County = @County, Type = @Type, Date = @Date WHERE (IncidentNumber = @IncidentNumber)";

            DELETE and UPDATE do not work.... uhmm

            modified on Tuesday, May 20, 2008 11:47 PM

            K Offline
            K Offline
            keyboard warrior
            wrote on last edited by
            #8

            In your code in there add this: DataKeyNames="IncidentNumber" like this: <asp:GridView ID="GridView1" runat="server" DataKeyNames="IncidentNumber" DataSourceID="SqlDataSource1" GridLines="None"> now try it.

            ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

            L 2 Replies Last reply
            0
            • K keyboard warrior

              In your code in there add this: DataKeyNames="IncidentNumber" like this: <asp:GridView ID="GridView1" runat="server" DataKeyNames="IncidentNumber" DataSourceID="SqlDataSource1" GridLines="None"> now try it.

              ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              awesome! your a life saver!

              1 Reply Last reply
              0
              • K keyboard warrior

                In your code in there add this: DataKeyNames="IncidentNumber" like this: <asp:GridView ID="GridView1" runat="server" DataKeyNames="IncidentNumber" DataSourceID="SqlDataSource1" GridLines="None"> now try it.

                ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #10

                Ok now my time search isnt working... You enter lets say: 1/1/2008 and 5/5/2008 and click a button.. here is the command:

                SqlDataSource1.SelectCommand = String.Format("SELECT * FROM [Clipboard] WHERE [Date] Between {0} AND {1}", Convert.ToDateTime(txtBeginningDate.Text), Convert.ToDateTime(txtEndDate.Text));

                But I get an Incorrect Syntax at 12?

                K 1 Reply Last reply
                0
                • L Lost User

                  Ok now my time search isnt working... You enter lets say: 1/1/2008 and 5/5/2008 and click a button.. here is the command:

                  SqlDataSource1.SelectCommand = String.Format("SELECT * FROM [Clipboard] WHERE [Date] Between {0} AND {1}", Convert.ToDateTime(txtBeginningDate.Text), Convert.ToDateTime(txtEndDate.Text));

                  But I get an Incorrect Syntax at 12?

                  K Offline
                  K Offline
                  keyboard warrior
                  wrote on last edited by
                  #11

                  in the sql server. in the "Clipboard" table...what is the data type of the "date" column... actually change of plans try this. SELECT * FROM [Clipboard] WHERE [Date] < {0} AND [Date] > {1} or SELECT * FROM [Clipboard] WHERE [Date] > {0} AND [Date] < {1}

                  ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

                  modified on Wednesday, May 21, 2008 12:30 AM

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    I am having problems getting this to work. I have it where it inserts the data from some text boxes above. So It Inserts and Deletes just fine. When it comes to updating and search for in between dates I have problems. Ok in the dialog for the update command it has the command for updating the fields. So when you click update in the grid view where are the commands that make it update? It will not update. When you change something and click UPDATE it just goes right back to what it was. Also how do I search for between dates? I have two text boxes and set the SQLdatasource1.SelectCommand = String.Format("SELECT * From [Clipboard] WHERE [Date] Between {0} AND {1}", txtBeginningDate.Text, txtEndDate.Text); Now when you enter 05-05-2008 in the text boxes and hit my SEARCH button it says: Failed to convert nvarchar to int. Where is it converting to INT? I have it set to nvarchar??? BTW I am using MVS 2008 Pro, and I am not writing a lot of these commands. It is generated for me but I can't seem to get it to do those things.

                    K Offline
                    K Offline
                    kinnuP
                    wrote on last edited by
                    #12

                    hi Jacob Dixon,"Ok in the dialog for the update command it has the command for updating the fields. So when you click update in the grid view where are the commands that make it update? It will not update. When you change something and click UPDATE it just goes right back to what it was." for this please check the isnotpsotback property is given in Pageload event or not .

                    L 1 Reply Last reply
                    0
                    • K kinnuP

                      hi Jacob Dixon,"Ok in the dialog for the update command it has the command for updating the fields. So when you click update in the grid view where are the commands that make it update? It will not update. When you change something and click UPDATE it just goes right back to what it was." for this please check the isnotpsotback property is given in Pageload event or not .

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      jgasm had me do something and it works great now. It was a DataKeyName value in the GridView code. I didn't have one set when I should of had it set to my ID Column. As for the date/time thing I have figured it out. Thanks!

                      1 Reply Last reply
                      0
                      • K keyboard warrior

                        in the sql server. in the "Clipboard" table...what is the data type of the "date" column... actually change of plans try this. SELECT * FROM [Clipboard] WHERE [Date] < {0} AND [Date] > {1} or SELECT * FROM [Clipboard] WHERE [Date] > {0} AND [Date] < {1}

                        ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

                        modified on Wednesday, May 21, 2008 12:30 AM

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        Hey! Yes I have the Date column set to: DateTime. It actually wants the Date and the Time but I am fine with that. It doesn't have an option for just the date. Anyways what I did was that I forgot the: " ' ' " between the beginning and ending dates. Like I did it like this: Where [Date] Between {0} AND {1} and it should of been: Where [Date] Between '{0}' AND '{1}' it works though. thanks!

                        K 1 Reply Last reply
                        0
                        • L Lost User

                          Hey! Yes I have the Date column set to: DateTime. It actually wants the Date and the Time but I am fine with that. It doesn't have an option for just the date. Anyways what I did was that I forgot the: " ' ' " between the beginning and ending dates. Like I did it like this: Where [Date] Between {0} AND {1} and it should of been: Where [Date] Between '{0}' AND '{1}' it works though. thanks!

                          K Offline
                          K Offline
                          keyboard warrior
                          wrote on last edited by
                          #15

                          i can't believe i didn't even notice that. i should have known better. it was late :(

                          ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

                          L 1 Reply Last reply
                          0
                          • K keyboard warrior

                            i can't believe i didn't even notice that. i should have known better. it was late :(

                            ----------------------------------------------------------- "When I first saw it, I just thought that you really, really enjoyed programming in java." - Leslie Sanford

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #16

                            lol! Its always the easiest problem that is the solution isnt it lol

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups