Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
1.00/5 (1 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:

C#
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!="")
            {
                try
                {
                    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);

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

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

                    con.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("error");
                }
                
            }
            else
            {
                MessageBox.Show("Please Provide Details!");
            }      
        }
Posted
Updated 21-Oct-16 22:51pm
v3
Comments
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?
[no name] 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?

Quote:
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.
 
Share this answer
 
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.

C#
try
{
    // your code here
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900