473,883 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Could not open Connection to SQL Server.

80 New Member
I'm getting this error.

SQLite error
near "16": syntax error

On the code:

Expand|Select|Wrap|Line Numbers
  1.  private void btnUpdate_Click(object sender, EventArgs e)
  2.         {
  3.             for (int i = 0; i < dataGridView1.Rows.Count; i++)
  4.             {
  5.                 string StrQuery = @"INSERT INTO Test VALUES (" + dataGridView1.Rows[i].Cells["Column1"].Value + ", " + dataGridView1.Rows[i].Cells["Column2"].Value + ", " + dataGridView1.Rows[i].Cells["Column3"].Value + ", " + dataGridView1.Rows[i].Cells["Column4"].Value + ", " + dataGridView1.Rows[i].Cells["Column5"].Value +");";
  7.                 try
  8.                 {
  9.                     SQLiteConnection conn = new SQLiteConnection(connString);
  10.                     conn.Open();
  11.                     using (SQLiteCommand comm = new SQLiteCommand(StrQuery, conn))
  12.                     {
  13.                         comm.ExecuteNonQuery();
  14.                     }
  15.                     conn.Close();
  16.                 }
  17.                 catch (Exception crap)
  18.                 {
  19.                     MessageBox.Show(crap.ToString());
  20.                 }
  21.             }
  22.         }
With breapoints etc it does show the values it has to insert into my database. But it just doesn't and gives me the Near "16" syntax error. Am I just beng stupid and overlooking something or is my code just totally wrong?
Feb 19 '13
50 4232
80 New Member
No I didn't change anything, I kept everything the way they I had them first (temp varChar solution lol)

The only thing I can think of is that the insert command is not correct for the way I'm trying to insert it, but I highly doubt that is the case as I've seen tons of tutorials & examples (Like yours Mikkeee) that use the same kind of Database and same method to insert.

I'll puzzle some more with it, but any help is much appreciated.
Feb 21 '13 #21
80 New Member
Ok so I changed my code a bit, and Now I don't get an input string error, actually I dont get an error at all. I only get this:

So it save something in my database but as soon as I click on Get DataBase it receives these numbers and not the actual records.

Modified code:
Expand|Select|Wrap|Line Numbers
  1.  string conString = "Data Source=D:/swdev/tapearchiver/Src/Tests Voor SQLdbs/TestingSQL3/test.s3db";
  3.             using (SQLiteConnection conn = new SQLiteConnection(conString))
  4.             {
  5.                 using (SQLiteCommand command = new SQLiteCommand())
  6.                 {
  7.                     conn.Open();
  8.                     command.Connection = conn;
  9.                     command.CommandText = "insert into Test ([Date], [LogName], [Channel], [DateRecord], [SizeInBytes]) values" + "(@Date, @LogName, @Channel, @DateRecord, @SizeInBytes)";
  10.                     SQLiteParameter[] param = new SQLiteParameter[5];
  12.                     param[0] = new SQLiteParameter("@Date", SqlDbType.VarChar);
  13.                     param[1] = new SQLiteParameter("@LogName", SqlDbType.VarChar);
  14.                     param[2] = new SQLiteParameter("@Channel", SqlDbType.VarChar);
  15.                     param[3] = new SQLiteParameter("@DateRecord", SqlDbType.VarChar);
  16.                     param[4] = new SQLiteParameter("@SizeInBytes", SqlDbType.Float);
  18.                     for (int i = 0; i < param.Length; i++)
  19.                     {
  20.                         command.Parameters.Add(param[i]);
  21.                     }
  23.                     command.CommandType = CommandType.Text;
  24.                     command.ExecuteNonQuery();  
  25.                }
  26.                 conn.Close();
  27.            } 
Feb 22 '13 #22
94 New Member
The values you see must be default values because nowhere in your code are you setting the Parameter.Value . You're now creating an array of parameters but they really aren't necessary. I'm not sure about the SQLite db but most db providers have the method 'Parameter.AddW ithValue' which is nice if you only have to insert a single record.
Feb 22 '13 #23
80 New Member
Yeah I can use the Parameters.AddW ithValue method. But I wouldnt know where to add it.

Would I have to change:
Expand|Select|Wrap|Line Numbers
  1.  command.Parameters.Add(new SQLiteParameter("@Date", SqlDbType.VarChar));
to ;

Expand|Select|Wrap|Line Numbers
  1. command.Parameters.AddWithValue(new SQLiteParameter("@Date", SqlDbType.VarChar));
But what do i add to the AddWithValue?
Feb 25 '13 #24
94 New Member
Look up your help file for AddwithValue to be sure but the line below works for all the data providers that I use. Also make sure to actually place the value you want stored in the parameter.

Expand|Select|Wrap|Line Numbers
  1. command.Parameters.AddWithValue("@Date", myDateVar);
Feb 25 '13 #25
80 New Member
Alright, well I'll get to that later. now I get some dumb happening lol. When I import my file it doesnt fill any column in the DataGridview itself other than Date and Channel. For the rest it says <Columnname> isn't unique.

EDIT: Never mind, just a small mistake I made the other day. I'm still trying to get the values added in to the database and be able to pick up the current value of the Database. Still playing with that at the moment.

Expand|Select|Wrap|Line Numbers
  1. Abort due to constraint violation
  2. column Date is not unique
I'm crying... how in hell is this not unique... Its torture I tell you!
Feb 25 '13 #26
94 New Member
You must have some type of constraint setup on your Date field. Is it a primary key, unique value, or a foreign key? If so, the value you're putting in the Date field must be unique.
Feb 25 '13 #27
80 New Member
So basically make it not unique in the database itself? I can do that even though I am feeling less compitent by the minute with this.
Even by making it not unique I get the exact same error... I don't like using database connections lol.

LOL I am amazing, I keep forgetting I have to edit the initial Database in SQLiteAdmin... Now I'm going to try to finish this and then win at being me.
Feb 25 '13 #28
94 New Member
Good Luck! And just remember that this stuff is like anything else in life. The more experience you have the easier it will be and you're definitely giving the SQLite data provider a good work out!
Feb 25 '13 #29
80 New Member
Exactly, I'm as far as I can insert into the database and get from the database. it doesnt insert what I want to though, same thing 22-22-22-6 for each column. Still having a think on how I can fix that.
Feb 25 '13 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

by: PAM | last post by:
Hello I'm a newbie trying to open a connection to an Excel file in a deployed ASP .NET app and get the error Dim MyCommand As New OleDbCommand(mySelectQuery, MyConnection MyConnection.Open(
by: Owen | last post by:
Hello: How know how many open connection have my application made in asp.net?, To know if a open a new connection and not close? Best regards. Owen.
by: Rafi B. | last post by:
Sorry for the English. I mean, when building my applications, I often need to do several database actions, and I'm thinking, what's better? 1) Open connection with DB, for instance mySQL, at beginning and close at the end, so every "unit" in the code knows that THIS IS the connection, and its opened. OR
by: Michael Swinarski | last post by:
I am working on setting up our new SQL Server 2005 servers. One of them will eventually be our Replication Distributor. However when I attempt to set this up within the wizard, I get the following error: ------------------------------ TITLE: New Publication Wizard ------------------------------ SQL Server could not configure 'Server\Instance' as a Distributor. ------------------------------
by: idsanjeev | last post by:
how to open connection in oracle for displying and inserting data in asp.net with vb
by: Bruno Marinho | last post by:
Hi, I'm having a problem creating an OU entry on a Open LDAP Server. I'm using the code below. $ad = ldap_connect("ldap://srvldap"); ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3); $bd = ldap_bind($ad,"CN=Admin,DC=subdomain,DC=domain,DC=com","secret"); $newou = "top"; $newou = "organizationalUnit";
by: Noorain | last post by:
hi, i create a php file. which function is: i)to refresh a certain time ii)create txt file with some query. i want to this php file always open in server and refresh after 5 minutes. i know how to refresh. i don't open this file in lacally. i want its open in server. please help me. thanks
by: KKAIYER | last post by:
I am using VB6,SQL Server,Crystall reports. when I run vb to print report crystal repots giving error 20599 cannot open SQL Server. what is the reason and what is the solution. Please help
by: vj4u | last post by:
i want to open sql server on button click in asp.net
by: Vinayak Pangam | last post by:
When I open the server it shows a duplicate name exists on the network. It affects the workstation also. I also check in all pc no duplicate name exits. Please give me a solution what is the exact problem
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.