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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. [Message Deleted]

[Message Deleted]

Scheduled Pinned Locked Moved C#
28 Posts 5 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.
  • V Vivek Vijayan

    if i am writing code for try{} i know how to write catch and finally tnx

    This code was posted by me...

    M Offline
    M Offline
    musefan
    wrote on last edited by
    #15

    Vivek Vijayan wrote:

    if i am writing code for try{} i know how to write catch and finally

    Er... Where is the logic in that? you are writing code for database insert but that evidently does not mean you know how to write it And it's a good example of why your need to make your example better in order to get a good response - you didn't put the catch/finally in your example so how do I know that is not your error?! Hmmm... a little thought can go a long way.

    Life goes very fast. Tomorrow, today is already yesterday.

    1 Reply Last reply
    0
    • V Vivek Vijayan

      SqlConnection con = connection(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = query; cmd.Connection = con; try { con.Open(); cmd.ExecuteNonQuery(); return 1; } catch (Exception e) { return 0; } finally { con.Close(); cmd.Dispose(); } tis is the code its running fine without exception but no data is inserted into the table at last

      This code was posted by me...

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #16

      Obviously there is no exception that what a try catch does, it enables you to handle the exception, you catch is returning 0 so your not going to see any problem. You may have a problem with your insert statement too, have your tried specifying the columns you are trying to add data to?

      Life goes very fast. Tomorrow, today is already yesterday.

      V 1 Reply Last reply
      0
      • V Vivek Vijayan

        SqlConnection con = connection(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = query; cmd.Connection = con; try { con.Open(); cmd.ExecuteNonQuery(); return 1; } catch (Exception e) { return 0; } finally { con.Close(); cmd.Dispose(); } tis is the code its running fine without exception but no data is inserted into the table at last

        This code was posted by me...

        M Offline
        M Offline
        moon_stick
        wrote on last edited by
        #17

        What values does this method return? You're catching the exception but not doing anything with it (apart from changing the return value). Assuming you're using VS, set a breakpoint on the line "return 0;" and check to see that the value of 'e' (the exception) is.

        It definitely isn't definatley

        V 1 Reply Last reply
        0
        • M moon_stick

          With regards to the query, I meant the actual string value that is being passed to the database, rather than the statement you use to construct it. Incidentally, is 'mydb' the name of the table or the name of a database containing the table (i.e. should it actually be "insert into mydb.dbo.mytable..."?). What about the rest of the questions I asked? Have you tried running a profiler on the database to see if the query is actually received. Does the c# statement actually exectute?

          It definitely isn't definatley

          V Offline
          V Offline
          Vivek Vijayan
          wrote on last edited by
          #18

          no exception is caught i don knwo where is the problem what is the meaning of this "Have you tried running a profiler on the database to see if the query is actually received. "

          This code was posted by me...

          M 1 Reply Last reply
          0
          • V Vivek Vijayan

            i don feel that pasting the code for close is necessary....

            This code was posted by me...

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #19

            Vivek Vijayan wrote:

            i don feel that pasting the code for close is necessary....

            Your right, it is not necessary... when you provide a better description of what the problem is. But as everyone is having to guess how bad your code is, people are going to ask questions.

            Life goes very fast. Tomorrow, today is already yesterday.

            V 1 Reply Last reply
            0
            • V Vivek Vijayan

              no exception is caught i don knwo where is the problem what is the meaning of this "Have you tried running a profiler on the database to see if the query is actually received. "

              This code was posted by me...

              M Offline
              M Offline
              moon_stick
              wrote on last edited by
              #20

              A database profiler is a tool which monitors a database and reports back when certain events occur, such as connections to the database, queries, triggers etc. SQL Server comes with its own profiler called (originally enough) SQL Server Profiler - I don't tend to use other databases but I'm sure there are similar tools available for other platforms.

              It definitely isn't definatley

              V 1 Reply Last reply
              0
              • M musefan

                Obviously there is no exception that what a try catch does, it enables you to handle the exception, you catch is returning 0 so your not going to see any problem. You may have a problem with your insert statement too, have your tried specifying the columns you are trying to add data to?

                Life goes very fast. Tomorrow, today is already yesterday.

                V Offline
                V Offline
                Vivek Vijayan
                wrote on last edited by
                #21

                yes i tried specifying the columns but no use any prob with insert query????? String query = "insert into mydb(username,password) values('" + textBox1.Text + "','" + textBox2.Text + "')";

                This code was posted by me...

                M 1 Reply Last reply
                0
                • M moon_stick

                  What values does this method return? You're catching the exception but not doing anything with it (apart from changing the return value). Assuming you're using VS, set a breakpoint on the line "return 0;" and check to see that the value of 'e' (the exception) is.

                  It definitely isn't definatley

                  V Offline
                  V Offline
                  Vivek Vijayan
                  wrote on last edited by
                  #22

                  i debuggged the code but no exception is caught may be prob with insert statemnt??? any prob with insert query????? String query = "insert into mydb(username,password) values('" + textBox1.Text + "','" + textBox2.Text + "')";

                  This code was posted by me...

                  M 1 Reply Last reply
                  0
                  • M musefan

                    Vivek Vijayan wrote:

                    i don feel that pasting the code for close is necessary....

                    Your right, it is not necessary... when you provide a better description of what the problem is. But as everyone is having to guess how bad your code is, people are going to ask questions.

                    Life goes very fast. Tomorrow, today is already yesterday.

                    V Offline
                    V Offline
                    Vivek Vijayan
                    wrote on last edited by
                    #23

                    this community is for helping not for findng mistakes only

                    This code was posted by me...

                    M 1 Reply Last reply
                    0
                    • V Vivek Vijayan

                      yes i tried specifying the columns but no use any prob with insert query????? String query = "insert into mydb(username,password) values('" + textBox1.Text + "','" + textBox2.Text + "')";

                      This code was posted by me...

                      M Offline
                      M Offline
                      musefan
                      wrote on last edited by
                      #24

                      try... INSERT INTO mydb ([username],[password]) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "') Also... - Confirm that 'mydb' is a table name and not the name of your database. - make sure your database columns have the corrent datatype - look into using parameters

                      Life goes very fast. Tomorrow, today is already yesterday.

                      1 Reply Last reply
                      0
                      • M moon_stick

                        A database profiler is a tool which monitors a database and reports back when certain events occur, such as connections to the database, queries, triggers etc. SQL Server comes with its own profiler called (originally enough) SQL Server Profiler - I don't tend to use other databases but I'm sure there are similar tools available for other platforms.

                        It definitely isn't definatley

                        V Offline
                        V Offline
                        Vivek Vijayan
                        wrote on last edited by
                        #25

                        do we use cmd.ExecuteNonQuery() for inserting??

                        This code was posted by me...

                        M 1 Reply Last reply
                        0
                        • V Vivek Vijayan

                          i debuggged the code but no exception is caught may be prob with insert statemnt??? any prob with insert query????? String query = "insert into mydb(username,password) values('" + textBox1.Text + "','" + textBox2.Text + "')";

                          This code was posted by me...

                          M Offline
                          M Offline
                          moon_stick
                          wrote on last edited by
                          #26

                          Try changing your code to this:

                          SqlConnection con = connection();
                          SqlCommand cmd = new SqlCommand();
                          cmd.CommandText = query;
                          cmd.Connection = con;
                          con.Open();
                          cmd.ExecuteNonQuery();
                          con.Close();
                          cmd.Dispose();

                          and see if you get an exception - if the insert has failed, the database should indicate this.

                          It definitely isn't definatley

                          1 Reply Last reply
                          0
                          • V Vivek Vijayan

                            do we use cmd.ExecuteNonQuery() for inserting??

                            This code was posted by me...

                            M Offline
                            M Offline
                            moon_stick
                            wrote on last edited by
                            #27

                            Yes that's fine. Broadly speaking, you use ExecuteNonQuery() to execute a SQL statement that doesn't return a result set, ExecuteQuery() to execute a statement that returns tabular data and ExecuteScalar() to execute a query which returns a single value (i.e. if you've done a count(*) or other scalar operation).

                            It definitely isn't definatley

                            1 Reply Last reply
                            0
                            • V Vivek Vijayan

                              this community is for helping not for findng mistakes only

                              This code was posted by me...

                              M Offline
                              M Offline
                              musefan
                              wrote on last edited by
                              #28

                              Vivek Vijayan wrote:

                              this community is for helping not for findng mistakes only

                              Finding mistakes is helping

                              Life goes very fast. Tomorrow, today is already yesterday.

                              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