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);

   }

}

 

Im trying to input the following data to my SQL server DB using C# but i get this exception thrown saying: Named Pipes Provider, error 40 - Could not open a connection to' SQL Server'. i did a google search on this and they recommend checking the connection string or restarting sql server service. i know my sql server db is communicating fine with my visual studio as i have a login form before this one and i use the DB for comparing data. so i did not go to restart the sql server service. i have check my connection string and it is correct. i have gone through every single word in the code below but i have failed to spot any mistakes. can u find anything wrong?

  private void button1_Click(object sender, EventArgs e)
        {
            try {
                int n = 1;
                string name = "sk";
                string pt = "test";
                SqlConnection cn = new SqlConnection("Data Source = HP - Q6RS5H5; Initial Catalog = proj; 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 Image(ID, Name, path) VALUES (@p, @nam, @pat)"; 
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

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

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't see authorization credentials in your connection string?

Try this:

https://www.connectionstrings.com/sql-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

Just now, Shammikit said:

i do not have any passwords set up for my SQL server and im using windows authentication.

how do you connect from SQL Management Studio?
Can you post a screenshot?

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:

how do you connect from SQL Management Studio?
Can you post a screenshot?

 

ms12.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Shammikit said:

 

ms12.PNG

you did notice that your C# has a connection to "HP - Q6RS5H5", right?

wanna try

new SqlConnection("Data Source= DESKTOP-Q6RS5H5; Database = proj; Integrated Security = True");

instead?

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

1 minute ago, DXMember said:

you did notice that your C# has a connection to "HP - Q6RS5H5", right?

wanna try

new SqlConnection("Data Source= DESKTOP-Q6RS5H5; Database = proj; Integrated Security = True");

instead?

OH! hold on

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Shammikit said:

OH! hold on

lol

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:

lol

nah man.still no good. this is the exception thrown:

 

er.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Shammikit said:

nah man.still no good. this is the exception thrown:

 

er.PNG

do what the error message says - add an allow rule for 1433 port in Windows Firewall, make sure your server has "Remote connections" and "Named pipes" and service is running

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

try replacing "Data Source = " with "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

Try opening a command prompt and try

sqlcmd -S .\ -q "SELECT COUNT(*) FROM IMAGE;"

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 replacing "Data Source = " with "Server = "

it gives the same error. as u stated before im going to check that firewall thing. however as i have stated before when i posted the question, im able to access my db during the login form which comes before this form. however that login form has a select query only. because this has a insert query is there some way these firewalls behave. like just because a select query works does it mean other queries like insert and update cannot work.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, DXMember said:

Try opening a command prompt and try


sqlcmd -S .\ -q "SELECT COUNT(*) FROM IMAGE;"

I got this. any idea what it means?

CMD.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Shammikit said:

it gives the same error. as u stated before im going to check that firewall thing. however as i have stated before when i posted the question, im able to access my db during the login form which comes before this form. however that login form has a select query only. because this has a insert query is there some way these firewalls behave. like just because a select query works does it mean other queries like insert and update cannot work.

try doing it from command prompt with sqlcmd

does it really work if you replace the Insert statement with SQL?

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

1 minute ago, Shammikit said:

I got this. any idea what it means?

CMD.PNG

try SELECT COUNT(*) FROM [Database Name].Image

but the connection seemed to work anyway...

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:

try SELECT COUNT(*) FROM [Database Name].Image

but the connection seemed to work anyway...

I got this:

mcd1.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Shammikit said:

I got this:

mcd1.PNG

is it saying i dont have such table in the DB?

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Shammikit said:

is it saying i dont have such table in the DB?

yeah, pretty much

 

SELECT COUNT(*) FROM Finalproj.dbo.Image

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

7 hours ago, DXMember said:

yeah, pretty much

 


SELECT COUNT(*) FROM Finalproj.dbo.Image

But i have a table called image in my DB!

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, Shammikit said:

Im trying to input the following data to my SQL server DB using C# but i get this exception thrown saying: Named Pipes Provider, error 40 - Could not open a connection to' SQL Server'. i did a google search on this and they recommend checking the connection string or restarting sql server service. i know my sql server db is communicating fine with my visual studio as i have a login form before this one and i use the DB for comparing data. so i did not go to restart the sql server service. i have check my connection string and it is correct. i have gone through every single word in the code below but i have failed to spot any mistakes. can u find anything wrong?


  private void button1_Click(object sender, EventArgs e)
        {
            try {
                int n = 1;
                string name = "sk";
                string pt = "test";
                SqlConnection cn = new SqlConnection("Data Source = HP - Q6RS5H5; Initial Catalog = proj; 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 Image(ID, Name, path) VALUES (@p, @nam, @pat)"; 
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

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

 

try adding your c# editor exe to firewall exemptions/allowed list

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Shammikit said:

But i have a table called image in my DB!

did you try adding "dbo." before your table name?

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 hours ago, DXMember said:

did you try adding "dbo." before your table name?

after typing that command it took a few seconds to process and i got this output:

cm.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Shammikit said:

after typing that command it took a few seconds to process and i got this output:

cm.PNG

there should be a dot before backslash and after the space that follows "-S" - should look like "-S .\"

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

1 hour ago, DXMember said:

there should be a dot before backslash and after the space that follows "-S" - should look like "-S .\"

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

sa.PNG

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

×