Click here to Skip to main content
13,868,400 members
Rate this:
Please Sign up or sign in to vote.
See more:
can anyone help me please?
I was unable to display the data.
please correct the try and catch statement

What I have tried:

private void btn_Save_Click(object sender, EventArgs e)
            string date = dateTimePicker1.Text;
            string taskName = txt_TaskName.Text;
            string notes = txt_Notes.Text;

            if(txt_TaskName.Text!="" && txt_Notes.Text!="")
                    String str = "Data Source=DESKTOP-CCDDCMC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
                    String query = "insert into tbl_Task(t_taskName,t_date,t_notes) values('" + taskName + "','" + date + "','" + notes + "')";
                    SqlConnection con = new SqlConnection(str);
                    SqlCommand cmd = new SqlCommand(query, con);

                    int result = cmd.ExecuteNonQuery();
                    if (result > 0)
                        MessageBox.Show("Data Saved Successfully.....");

                        txt_TaskName.Text = "";
                        txt_Notes.Text = "";
                        dateTimePicker1.Text = "";

                catch (Exception)
                MessageBox.Show("Please Provide Details!");
Updated 21-Oct-16 23:51pm
Suvendu Shekhar Giri 21-Oct-16 17:20pm
Sharing the whole program or application will not help us understanding your issue.
Which portion of your code has issue?
NotPolitcallyCorrect 21-Oct-16 17:21pm
Learn to use the debugger.
ChrisHD22 21-Oct-16 17:41pm
As NotPolliticallyCorrect said, use the debugger to know which lines of code are creating conflict and what is the error thrown. Then you should post the question saying what is the problem occurring in your project.
Richard MacCutchan 22-Oct-16 4:26am
Stop using string concatenation on your INSERT statement and use proper parameterised queries. At the very least you will be able to see what is happening better.
Karthik_Mahalingam 27-Oct-16 1:45am
error message?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Not displaying data which I stored using database in C#
Here you 2 questions.
Either ypu fail to recover the data, or you fail to display the data.
The debugger will show what your code does, it will help you to spot the problem.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here's a small fix that will show what the error is.
You are only showing in the message box the word "error"
Show the error message from the exception.
However, you need to get to know your debugger.

The debugger is one of the most important tools you have.

    // your code here
catch (Exception ex)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Few points:
- You don't dispose Sql* objects, this should be done when they are no longer needed
- You don't use parameters, so you're vulnerable to SQL injection and type conversion problems
- you hide the actual exception and just show that something went wrong, instead show the actual reason

I suggest going through Properly executing database operations[^] in which these problems are addressed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190214.1 | Last Updated 22 Oct 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100