Saving to a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mercea
    New Member
    • Aug 2007
    • 35

    Saving to a database

    <edit by Frinavale>
    Removed the quoted .NET article on how to use a database in your program
    </edit>
    PLS HELP!
    I Have been able to do all the above successfully though for my own code(C#), i'm retreiving the parameters from the user's input i.e. a login form. when i run this code it saves nothing to the database and it generates no errors.so i dont know where i've gone wrong.
    i put the following code in the onclick eventhandler of the login button but it still doesnt work. can u help me?
    [code=cpp]
    public void login1_Click(ob ject sender, EventArgs e)
    {


    addstudentinfo. InsertParameter s["Surname"].DefaultValue =
    TextBox1.Text.T oString();
    addstudentinfo. InsertParameter s["Names"].DefaultValue
    =
    TextBox2.Text.T oString();
    addstudentinfo. InsertParameter s["Regno"].DefaultValue
    =
    TextBox3.Text.T oString();
    }[/code]
    Last edited by Frinavale; Aug 17 '07, 01:05 PM. Reason: Removed quote of an article, added code tags to make more legible
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by mercea
    <edit by Frinavale>
    Removed the quoted .NET article on how to use a database in your program
    </edit>
    PLS HELP!
    I Have been able to do all the above successfully though for my own code(C#), i'm retreiving the parameters from the user's input i.e. a login form. when i run this code it saves nothing to the database and it generates no errors.so i dont know where i've gone wrong.
    i put the following code in the onclick eventhandler of the login button but it still doesnt work. can u help me?
    [code=cpp]
    public void login1_Click(ob ject sender, EventArgs e)
    {


    addstudentinfo. InsertParameter s["Surname"].DefaultValue =
    TextBox1.Text.T oString();
    addstudentinfo. InsertParameter s["Names"].DefaultValue
    =
    TextBox2.Text.T oString();
    addstudentinfo. InsertParameter s["Regno"].DefaultValue
    =
    TextBox3.Text.T oString();
    }[/code]
    Hi Mercea,

    I have split your question off of the "how to use a database in your program" article and have moved it to the .NET Forum. In the future please post your questions here (blue menu: Forum-> .NET). Thanks.

    Looking at your button click code I'm not seeing any sort of database manipulation. Could you please post the code that is causing the problem so that we can help you better.

    Cheers!

    -Frinny

    Comment

    • mercea
      New Member
      • Aug 2007
      • 35

      #3
      thanks.
      the code above is the code on the login page.
      the code for the gridview page is

      Code:
      protected void Page_Load(object sender, EventArgs e)
          {
              SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
              SqlCommand cmnd = new SqlCommand("SELECT * FROM test101",conn);
      
             
              
                  conn.Open();
                 
      
                  GridView1.DataSource = cmnd.ExecuteReader();
      
                  GridView1.DataBind();
      
                  conn.Close();
              
              
               
              
          }
      The gridview gets its data from an sqldatasource whose select and insert statements have been defined
      Code:
      <asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO EEEN501(ID,Surname, Names,Regno)
      VALUES (@TextBox1, @TextBox2, @TextBox3)"
                  ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
                
                      <InsertParameters>
                          <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
                              PropertyName="Text" Size="50" Type="String" />
                          <asp:ControlParameter ControlID="TextBox2" DefaultValue="TextBox2.Text" Name="Names"
                              PropertyName="Text" Size="50" Type="String" />
                          <asp:ControlParameter ControlID="TextBox3" DefaultValue="TextBox3.Text" Name="Regno"
                              PropertyName="Text" Size="9" Type="String" />
                      </InsertParameters>
                  </asp:SqlDataSource>

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Are you ever calling the Insert() fnuction?
        Performs an insert operation using the InsertCommand SQL string and any parameters that are in the InsertParameters collection.


        My help said that you have to call that in order to run your insertcommand

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Hi Mercea,

          I think i know what is happening.
          Currently you are setting your GridView's data source in the Page_Load() function like so:
          [CODE=vbnet]
          protected void Page_Load(objec t sender, EventArgs e) {
          SqlConnection conn = new SqlConnection(" Data Source=(local); Initial Catalog=enginee ring; Integrated Security=True") ;
          SqlCommand cmnd = new SqlCommand("SEL ECT * FROM test101",conn);
          conn.Open();
          GridView1.DataS ource = cmnd.ExecuteRea der();
          GridView1.DataB ind();
          conn.Close();
          }
          [/CODE]

          Because of this your GridView's data source is reset every time your user posts back.
          If you do this, then your edit details will be over written with the data from your database and the user input values during editing will be lost.

          You should only do this when it is not postback or if any changes have been made so to refresh your GridView.

          Try changing your code to this:
          [CODE=vbnet]
          protected void Page_Load(objec t sender, EventArgs e) {
          If (IsPostBack == False)
          {
          SqlConnection conn = new SqlConnection(" Data Source=(local); Initial Catalog=enginee ring; Integrated Security=True") ;
          SqlCommand cmnd = new SqlCommand("SEL ECT * FROM test101",conn);
          conn.Open();
          GridView1.DataS ource = cmnd.ExecuteRea der();
          GridView1.DataB ind();
          conn.Close();
          }
          }
          [/CODE]

          Hopefully this solves your problem :)

          -Frinny

          Comment

          • mercea
            New Member
            • Aug 2007
            • 35

            #6
            Originally posted by Frinavale
            Hi Mercea,

            I think i know what is happening.
            Currently you are setting your GridView's data source in the Page_Load() function like so:
            [CODE=vbnet]
            protected void Page_Load(objec t sender, EventArgs e) {
            SqlConnection conn = new SqlConnection(" Data Source=(local); Initial Catalog=enginee ring; Integrated Security=True") ;
            SqlCommand cmnd = new SqlCommand("SEL ECT * FROM test101",conn);
            conn.Open();
            GridView1.DataS ource = cmnd.ExecuteRea der();
            GridView1.DataB ind();
            conn.Close();
            }
            [/CODE]

            Because of this your GridView's data source is reset every time your user posts back.
            If you do this, then your edit details will be over written with the data from your database and the user input values during editing will be lost.

            You should only do this when it is not postback or if any changes have been made so to refresh your GridView.

            Try changing your code to this:
            [CODE=vbnet]
            protected void Page_Load(objec t sender, EventArgs e) {
            If (IsPostBack == False)
            {
            SqlConnection conn = new SqlConnection(" Data Source=(local); Initial Catalog=enginee ring; Integrated Security=True") ;
            SqlCommand cmnd = new SqlCommand("SEL ECT * FROM test101",conn);
            conn.Open();
            GridView1.DataS ource = cmnd.ExecuteRea der();
            GridView1.DataB ind();
            conn.Close();
            }
            }
            [/CODE]

            Hopefully this solves your problem :)

            -Frinny
            hi,
            thanks for replying but it still does not save any data to the database. the gridview page comes up blank and does not even display the emptydatatempla te as before. any more suggestions?

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Did you ever call the insert function?

              Comment

              • mercea
                New Member
                • Aug 2007
                • 35

                #8
                Originally posted by Plater
                Did you ever call the insert function?
                anytime i called an insert function i.e. addstudentinfo. Insert(), when i try to run it, it gives an error "must declare scalar variable @TextBox1"
                and i have declared my variables. when configuring my datasource and calling the insertparameter s functions. So i took it out

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Well your inserts will never happen without calling the insert().
                  That bit of info would have been usefull awhile ago. There is something wrong with sql command string (it's never getting a value for that varriable)

                  Work on fixing that and see what else it fixes.

                  Comment

                  • mercea
                    New Member
                    • Aug 2007
                    • 35

                    #10
                    Originally posted by Plater
                    Well your inserts will never happen without calling the insert().
                    That bit of info would have been usefull awhile ago. There is something wrong with sql command string (it's never getting a value for that varriable)

                    Work on fixing that and see what else it fixes.
                    sorry for leaving out that important piece of information.
                    pls would u mind taking a look at code and just pointing me to were i've missed it? cos i dont know were i've gone wrong or wat i've missed
                    This is for configuration of the sqldatasource
                    Code:
                    asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno)
                    VALUES (@TextBox1, @TextBox2, @TextBox3)"
                                ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
                              
                                    <InsertParameters>
                                        <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
                                            PropertyName="Text" Size="50" Type="String" />
                                        <asp:ControlParameter ControlID="TextBox2" DefaultValue="TextBox2.Text" Name="Names"
                                            PropertyName="Text" Size="50" Type="String" />
                                        <asp:ControlParameter ControlID="TextBox3" DefaultValue="TextBox3.Text" Name="Regno"
                                            PropertyName="Text" Size="9" Type="String" />
                                    </InsertParameters>
                                </asp:SqlDataSource>
                    this is for the gridview
                    Code:
                    <asp:GridView ID="GridView1"
                                runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
                                AutoGenerateEditButton="True"  AllowSorting="True" BackColor="LightGoldenrodYellow" 
                                BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" PageSize="20" 
                                Height="374px" EmptyDataText="null" DataSourceID="addstudentinfo"     >
                                <Columns>
                                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                                        SortExpression="ID" />
                                    <asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
                                    <asp:BoundField DataField="Names" HeaderText="Names" SortExpression="Names" />
                                    <asp:BoundField DataField="Registration" HeaderText="Registration" SortExpression="Registration" />
                                    <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
                                    
                                </Columns>
                    </GridView>
                    <asp:SqlDataSource ID="addstudentinfo" runat="server" ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>"
                               SelectCommand="SELECT * FROM [test101]" DataSourceMode="DataSet"
                               InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno)
                    VALUES (@TextBox1, @TextBox2, @TextBox3)">
                               
                               </asp:SqlDataSource>
                    the C# code is as above.
                    i'm sure i've missed it somewhere.
                    thanks

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      Well take a look at these:
                      Code:
                      asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)"
                                  ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
                                
                                      <InsertParameters>
                                          <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
                      Your insert command uses "Textbox1" (and 2 and 3)
                      Then your insert parameters have ControlID="Text Box1" and then a DefaultValue="T extBox1.Text", that seems fishy. Like you have two controls with the id TextBox1 or something.
                      You should go through and try and give everything unique descriptive names.

                      Comment

                      • mercea
                        New Member
                        • Aug 2007
                        • 35

                        #12
                        Originally posted by Plater
                        Well take a look at these:
                        Code:
                        asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)"
                                    ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
                                  
                                        <InsertParameters>
                                            <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
                        Your insert command uses "Textbox1" (and 2 and 3)
                        Then your insert parameters have ControlID="Text Box1" and then a DefaultValue="T extBox1.Text", that seems fishy. Like you have two controls with the id TextBox1 or something.
                        You should go through and try and give everything unique descriptive names.
                        sorry to ask so many questions but i thought that since the variable is to be read from the first TextBox, whenever i'm referencing like in the insert command and setting the default value it i have to use its id(which is TextBox1). do u mean i should change the id of the textboxes all together?

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          To be honest I'm shooting in the dark here, I normally do everything in code and not as embeded objects in UI so I'm not familiar with their command structure.

                          It just seemed like you were naming possibly multiple things with the same ID and then your insert parameter couldn't find it for some reason.
                          So if you named everything something unique, you could see which one was causing the problem.

                          Comment

                          • kenobewan
                            Recognized Expert Specialist
                            • Dec 2006
                            • 4871

                            #14
                            Originally posted by Plater
                            To be honest I'm shooting in the dark here
                            Now I understand why people are scared of the dark ;).

                            Comment

                            • mercea
                              New Member
                              • Aug 2007
                              • 35

                              #15
                              Originally posted by kenobewan
                              Now I understand why people are scared of the dark ;).
                              CAN U BRING US INTO THE LIGHT? COS THIS DARKNESS IS REALLY THICK :(

                              Comment

                              Working...