Resolved Error add Image to SQL database

Dwhite

Member
Joined
Nov 10, 2020
Messages
17
Programming Experience
1-3
Evening All,

Hope all is well.
I'm currently building a windows form where it takes images and uploads them to a sql database. For the most part i believe i added the correct parameters to add images but are getting an error of "invalid object name" which is referring to the db name. i've checked it is the correct name. I wanted to see if anyone can take a look at my code and see if they can spot an issue. I believe im overlooking something but cant figure it out.
This is the first DB i've done working with images. i normally build with entry data. I've attached a screenshot. Please let me know if you can assist.

Thank you,

DWhite
 

Attachments

  • vbsql.JPG
    vbsql.JPG
    53.9 KB · Views: 12
Solution
As for the issue, the problem is that you're not specifying a database to connect to at all. When you don't specify a database in your connection string, why would it connect to your database? It will connect to the master database on the specified instance (Workstation\SQLEXPRESS) if you don't specify some other database and that database contains no table named RTLS. You need to change your connection string to specify the database you want to connect to, e,g,
VB.NET:
Dim connection As New SqlConnection("Data Source=Workstation\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True")
Please don't post just pictures of code. ALWAYS post the code as text, formatted as code, and then add a picture if and only if it adds value. Images can be hard to view, especially on mobile devices, and we cannot copy code from images if we want to test or quote it.
 
If SQL Server is telling you that an object name is invalid then that suggests that you have specified a table that doesn't exist. When you post the code properly, you should also specify which line throws the exception and the full error message, as well as any additional information provided by the exception. The site's code editor allows you to highlight one or more lines in your code so it's easy to indicate where the exception is thrown. An exception when you open a connection and an exception when you execute a command are two very different things. The error message sounds like the latter, in which case you have already connected to the database and so the database name is irrelevant.
 
Thank you for the feedback. The exception is thrown at the second to last line of the program when it executes the query (cmd2.ExecuteNonQuery). The full error is" System.Data.SqlClient.SqlException: 'Invalid object name 'RTLS'.'" which is the table name.
Please see the the code below:

VB.NET:
Dim connection As New SqlConnection("Data Source=Workstation\SQLEXPRESS;Integrated Security=True")

connection.Open()

Dim image1 As Image = Image.FromFile("C:\RTLSSCREENSHOT\" & Gethostname & ".png", SqlDbType.Image)
Dim ms As New System.IO.MemoryStream()

image1.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

Dim md As Byte() = ms.GetBuffer
Dim sql As String = "insert into RTLS(wsid,wsview) " & "values (@wsid,@wsview)"
Dim p As New SqlClient.SqlParameter("@wsid", Gethostname)
Dim p1 As New SqlClient.SqlParameter("@wsview", md)

p.Value = md

Dim cmd2 As New SqlClient.SqlCommand(sql, connection)

cmd2.Parameters.Add(p)
cmd2.Parameters.Add(p1)
cmd2.ExecuteNonQuery()
connection.Close()
 
Last edited by a moderator:
As for the issue, the problem is that you're not specifying a database to connect to at all. When you don't specify a database in your connection string, why would it connect to your database? It will connect to the master database on the specified instance (Workstation\SQLEXPRESS) if you don't specify some other database and that database contains no table named RTLS. You need to change your connection string to specify the database you want to connect to, e,g,
VB.NET:
Dim connection As New SqlConnection("Data Source=Workstation\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True")
 
Solution
Back
Top