Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am getting must declare the scalar variable but it is declared below

Can someone tell me what's wrong with this code.

thanks

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.IO;
using System.Data.SqlClient;
namespace homeWork2
{
    public partial class entry : Form
    {
        public entry()
        {
            InitializeComponent();
            
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            DialogResult result = op.ShowDialog();
            if (result == DialogResult.OK)
            {
                String file = op.FileName;
                Image im = Image.FromFile(file);
                this.pictureBox1.Image = im;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            DialogResult result = op.ShowDialog();
            if (result == DialogResult.OK)
            {
                String file = op.FileName;
                FileStream f = new FileStream(file, FileMode.Open);
                StreamReader sr = new StreamReader(f);
                string str = sr.ReadLine();
                string desc = sr.ReadToEnd();
                this.textBox1.Text = str;
                this.richTextBox1.Text = desc;
            }
           
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string p = this.pictureBox1.ImageLocation;
            string name = this.textBox1.Text;
            string description = this.richTextBox1.Text;

            DateTime da = new DateTime();
            string date = da.ToString("dd/MM/YYYY");
            string connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\Database1.mdf;Integrated Security=True";
            SqlConnection con = new SqlConnection(connString);
            string query = "insert into diaryDB(Title , Description , Date , pic) values (@name , @description ,@date , @p)";
            SqlCommand comm = new SqlCommand(query, con);

            SqlParameter p1 = new SqlParameter("Title", name);
            SqlParameter p2 = new SqlParameter("Description", description);
            SqlParameter p3 = new SqlParameter("Date", date);
            SqlParameter p4 = new SqlParameter("pic", p);

            comm.Parameters.Add(p1);
            comm.Parameters.Add(p2);
            comm.Parameters.Add(p3);
            comm.Parameters.Add(p4);
            con.Open();
            comm.ExecuteNonQuery();
            con.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            mainMenu m = new mainMenu();
            m.Show();
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            mainMenu m = new mainMenu();
            m.Show();
            this.Close();
        }
    }
}
Posted
Comments
PIEBALDconsult 23-Nov-14 12:37pm    
Your prameter names are inconsistent.

1 solution

replace
string query = "insert into diaryDB(Title , Description , Date , pic) values (@name , @description ,@date , @p)";
SqlCommand comm = new SqlCommand(query, con);
SqlParameter p1 = new SqlParameter("Title", name);
SqlParameter p2 = new SqlParameter("Description", description);
SqlParameter p3 = new SqlParameter("Date", date);
SqlParameter p4 = new SqlParameter("pic", p);

comm.Parameters.Add(p1);
comm.Parameters.Add(p2);
comm.Parameters.Add(p3);
comm.Parameters.Add(p4)

with
string query = "insert into diaryDB([Title] , [Description] , [Date] , pic) values (@name , @description ,@date , @p)";
SqlCommand comm = new SqlCommand(query, con);
//use the same name given in the sql statement as parameter name
comm.Parameters.AddWithValue("@name", name);
comm.Parameters.AddWithValue("@description", description);
comm.Parameters.AddWithValue("@date", DateTime.Now.ToString("dd/MM/YYYY"));
comm.Parameters.AddWithValue("@p", p);
 
Share this answer
 
v4
Comments
Maciej Los 23-Nov-14 13:22pm    
+5
DamithSL 23-Nov-14 13:26pm    
Thank you, Maciej

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