Jump to content

Cant find error in my sql statement

Go to solution Solved by DXMember,
18 minutes ago, Shammikit said:

i got a this now:

f.PNG

Modify your code to have commands in this order:

Before you execute your command, add this line:

 

cmd.Connection = cn;

 



private void button1_Click(object sender, EventArgs e)

{

   try

   {

      int n = 1;

      string name = "sk";

      string pt = "test";

      SqlConnection cn = new SqlConnection(@"Server = .\; Integrated Security = True");

      cmd.Parameters.Clear();

      cmd.Parameters.AddWithValue("@p", n);

      cmd.Parameters.AddWithValue("@nam", name);

      cmd.Parameters.AddWithValue("@pat", pt);

      cmd.CommandText = "INSERT INTO Finalproj.dbo.Image(ID, Name, path) VALUES (@p, @nam, @pat)";

      cn.Open();

      cmd.Connection = cn;

      cmd.ExecuteNonQuery();

      cn.Close();

   }

   catch (Exception ex)

   {

      string x = ex.ToString();

      MessageBox.Show(x);

   }

}

 

Just now, Shammikit said:

Oh. i got this.i think it found the table

sa.PNG

nice

 

SqlConnection cn = new SqlConnection("Server  = .\;");

cmd.CommandText = "INSERT INTO Finalproj.dbo.Image(ID, Name, path) VALUES (@p, @nam, @pat)"; 

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, DXMember said:

nice

 

 


SqlConnection cn = new SqlConnection("Server  = .\;");

 

 


cmd.CommandText = "INSERT INTO Finalproj.dbo.Image(ID, Name, path) VALUES (@p, @nam, @pat)"; 

i should type this in the CMD?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Shammikit said:

i should type this in the CMD?

no, try it in your program

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, DXMember said:

no, try it in your program

I tried it but still no good. getting same error. This is what the code looks like:

 private void button1_Click(object sender, EventArgs e)
        {
            try {
                int n = 1;
                string name = "sk";
                string pt = "ask";
                SqlConnection cn = new SqlConnection("Data Source = DESKTOP - Q6R55H5; Initial Catalog = Finalproj; Integrated Security = True");
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@p", n);
                cmd.Parameters.AddWithValue("@nam", name);
                cmd.Parameters.AddWithValue("@pat", pt);
                cmd.CommandText = "INSERT INTO Finalproj.dbo.Image(ID, Name, Filepath) VALUES (@p, @nam, @pat)";
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

            catch(Exception ex)
            {
                string x = ex.ToString();
                MessageBox.Show(x);
            }

I also tried this connection string: 
SqlConnection cn = new SqlConnection("Server  = DESKTOP - Q6R55H5; Initial Catalog = Finalproj; Integrated Security = True");

 

It didnt work as well

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Shammikit said:

I tried it but still no good. getting same error. This is what the code looks like:


 private void button1_Click(object sender, EventArgs e)
        {
            try {
                int n = 1;
                string name = "sk";
                string pt = "ask";
                SqlConnection cn = new SqlConnection("Data Source = DESKTOP - Q6R55H5; Initial Catalog = Finalproj; Integrated Security = True");
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@p", n);
                cmd.Parameters.AddWithValue("@nam", name);
                cmd.Parameters.AddWithValue("@pat", pt);
                cmd.CommandText = "INSERT INTO Finalproj.dbo.Image(ID, Name, Filepath) VALUES (@p, @nam, @pat)";
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

            catch(Exception ex)
            {
                string x = ex.ToString();
                MessageBox.Show(x);
            }

I also tried this connection string: 
SqlConnection cn = new SqlConnection("Server  = DESKTOP - Q6R55H5; Initial Catalog = Finalproj; Integrated Security = True");

 

It didnt work as well

try connection string like

SqlConnection cn = new SqlConnection("Server  = .\;");

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, DXMember said:

try connection string like

 


SqlConnection cn = new SqlConnection("Server  = .\;");

 

im not sure if this is the way u asked me to write.im getting a error somehow

a.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Shammikit said:

im not sure if this is the way u asked me to write.im getting a error somehow

a.PNG

do a double backslash there or add '@' before the quotes:

 

SqlConnection cn = new SqlConnection("Server  = .\\;");

SqlConnection cn = new SqlConnection(@"Server  = .\;");

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, DXMember said:

do a double backslash there or add '@' before the quotes:

 

SqlConnection cn = new SqlConnection("Server  = .\\;");

SqlConnection cn = new SqlConnection(@"Server  = .\;");

i tried both these methods now. got this error for both:

 

ab.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Shammikit said:

i tried both these methods now. got this error for both:

 

ab.PNG

Try adding that  Integrated Security = True; After semi-column in connection string

 

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, DXMember said:

Try adding that  Integrated Security = True; After semi-column in connection string

 

i got a this now:

f.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Shammikit said:

i got a this now:

f.PNG

Modify your code to have commands in this order:

Before you execute your command, add this line:

 

cmd.Connection = cn;

 



private void button1_Click(object sender, EventArgs e)

{

   try

   {

      int n = 1;

      string name = "sk";

      string pt = "test";

      SqlConnection cn = new SqlConnection(@"Server = .\; Integrated Security = True");

      cmd.Parameters.Clear();

      cmd.Parameters.AddWithValue("@p", n);

      cmd.Parameters.AddWithValue("@nam", name);

      cmd.Parameters.AddWithValue("@pat", pt);

      cmd.CommandText = "INSERT INTO Finalproj.dbo.Image(ID, Name, path) VALUES (@p, @nam, @pat)";

      cn.Open();

      cmd.Connection = cn;

      cmd.ExecuteNonQuery();

      cn.Close();

   }

   catch (Exception ex)

   {

      string x = ex.ToString();

      MessageBox.Show(x);

   }

}

 

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, DXMember said:

Modify your code to have commands in this order:

Before you execute your command, add this line:

 

cmd.Connection = cn;

 

IT WORKS!!!!! HAHAHA

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Shammikit said:

IT WORKS!!!!! HAHAHA

geee.... after two days

mark as solved and good luck, have fun

kek

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, DXMember said:

geee.... after two days

mark as solved and good luck, have fun

kek

Thanks a lot, and yea was stuck on this from yesterday morning. thanks once again for your time.:)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×