Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AddrApp
{
    public partial class Form1 : Form
    {
        SqlConnection con= new SqlConnection(@"Data Source=DESKTOP-5KNSK7E\SQLEXPRESS;Initial Catalog=church;Integrated Security=True");  
        SqlCommand cmd;  
        SqlDataAdapter adapt;  
        //ID variable used in Updating and Deleting Record  
        int ID = 0;  
        public Form1()  
        {  
            InitializeComponent();  
            DisplayData();  
        }
    }
    private void DisplayData()

    {
 	throw new NotImplementedException();
    }  

        private void btn_Insert_Click(object sender, EventArgs e)  
{
            if (txt_name.Text != "" && txt_address.Text != "" && txt_contact.Text != "" && txt_clergy.Text != "" && txt_cantor.Text != "" && txt_charges.Text != "" && txt_picture.Text != "")  
            {  
                cmd = new SqlCommand("insert into church(Name,Address,Contact,Clergy,Cantor,Charges,Picture) values(@Name,@Address,@Contact,@Clergy,@Cantor,@Charges,@Picture)", con);  
                con.Open();  
                cmd.Parameters.AddWithValue("@Name", txt_name.Text);  
                cmd.Parameters.AddWithValue("@Address", txt_address.Text);
                cmd.Parameters.AddWithValue("@Contact", txt_contact.Text);  
                cmd.Parameters.AddWithValue("@Clergy", txt_clergy.Text);
                cmd.Parameters.AddWithValue("@Cantor", txt_cantor.Text);
                cmd.Parameters.AddWithValue("@Charges", txt_charges.Text);  
                cmd.Parameters.AddWithValue("@Picture", txt_picture.Text); 
                cmd.ExecuteNonQuery();  
                con.Close();  
                MessageBox.Show("Record Inserted Successfully");  
                DisplayData();  
                ClearData();  
            }  
            else
            {  
                MessageBox.Show("Please Provide Details!");  
            }  
}
        //Display Data in DataGridView  
        private void DisplayData()  
        {  
            con.Open();  
            DataTable dt=new DataTable();  
            adapt=new SqlDataAdapter("select * from church",con);  
            adapt.Fill(dt);  
            dataGridView1.DataSource = dt;  
            con.Close();  
        }  
        //Clear Data  
        private void ClearData()  
        {  
            txt_name.Text = "";  
            txt_address.Text = "";
            txt_contact.Text = "";  
            txt_clergy.Text = ""; 
            txt_cantor.Text = "";  
            txt_charges.Text = ""; 
            txt_picture.Text = "";  
            ID = 0;  
        }  
        //dataGridView RowHeaderMouseClick Event  
        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)  
        {  
            ID = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());  
            txt_name.Text = dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();  
            txt_address.Text = dataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
            txt_contact.Text = dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();  
            txt_clergy.Text = dataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();  
            txt_cantor.Text = dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();  
            txt_charges.Text = dataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();   
            txt_picture.Text = dataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();     
        }  
        //Update Record  
        private void btn_Update_Click(object sender, EventArgs e)  
        {  
            if (txt_name.Text != "" && txt_address.Text != "" && txt_contact.Text != "" && txt_clergy.Text != "" && txt_cantor.Text != "" && txt_charges.Text != "" && txt_picture.Text != "")  
            {  
                cmd = new SqlCommand("update church set Name=@Name,Address=@Address,Contact=@Contact,Clergy=@Clergy,Cantor=@Cantor,Charges=@Charges,Picture=@Picture where ID=@ID", con);  
                con.Open();  
                cmd.Parameters.AddWithValue("@ID", ID);  
                cmd.Parameters.AddWithValue("@Name", txt_Name.name);  
                cmd.Parameters.AddWithValue("@Address", txt_address.Text);
                cmd.Parameters.AddWithValue("@Contact", txt_contact.Text);  
                cmd.Parameters.AddWithValue("@Clergy", txt_clergy.Text);  
                cmd.Parameters.AddWithValue("@Cantor", txt_cantor.Text);
                cmd.Parameters.AddWithValue("@Charges", txt_charges.Text);  
                cmd.Parameters.AddWithValue("@Picture", txt_picture.Text); 
                cmd.ExecuteNonQuery();  
                MessageBox.Show("Record Updated Successfully");  
                con.Close();  
                DisplayData();  
                ClearData();  
            }  
            else  
            {  
                MessageBox.Show("Please Select Record to Update");  
            }  
        }  
        //Delete Record  
        private void btn_Delete_Click(object sender, EventArgs e)  
        {  
            if(ID!=0)  
            {  
                cmd = new SqlCommand("delete church where ID=@ID",con);  
                con.Open();  
                cmd.Parameters.AddWithValue("@ID",ID);  
                cmd.ExecuteNonQuery();  
                con.Close();  
                MessageBox.Show("Record Deleted Successfully!");  
                DisplayData();  
                ClearData();  
            }  
            else  
            {  
                MessageBox.Show("Please Select Record to Delete");  
            } 

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


What I have tried:

Help, I have tried everything, cant seem to figure it out!!!!!

Error 5 Expected class, delegate, enum, interface, or struct LINE: 27, 30, 33, 37

Error 6 Type or namespace definition, or end-of-file expected LINE: 24,51
Posted
Updated 14-Oct-16 4:50am
v2
Comments
Philippe Mori 13-Oct-16 11:10am    
Put your code in a code block. You won't get much help with long, unformatted code.

Identify where those lines are. Do you really think, we will count the lines to be able to help you?
Philippe Mori 13-Oct-16 11:23am    
Also, without effort in you question, your rating will be low.
[no name] 13-Oct-16 11:11am    
Are we supposed to guess what line 27, 30, 33 and 37 is?
#realJSOP 13-Oct-16 12:32pm    
You haven't actually tried *anything*. The entire class should be highlighted in red in Visual Studio.

1 solution

If you would indent your code, it would be trivial to see the problem...

But having all lines start at column 0, it is hard to see mismatching { }.

C#
public Form1() 
 { 
 InitializeComponent(); 
 DisplayData(); 
 }
 }  // This would end the class !!!


And by the way, given the fact that the first error is on line 27. It is not very hard to find that the actual problem is just before.

Also, almost every serious editor allows to view matching braces. You just have to put your cursor on } and the editor would highlight the corresponding {.

And another trick when you have such problem is to comment out part of the code like anything between class { and }. Then you add code progressively.

But with modern editor, you also have a command to reformat your code and it would fix indentation thus making it easier to find the problem. However, the above trick of commenting might be useful for internal compiler error for example.
 
Share this answer
 
v4
Comments
#realJSOP 13-Oct-16 12:28pm    
I tried to format the code block, but CP kept crashing every time I tried.

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