Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Small project

Author  Topic 

nhighton
Starting Member

11 Posts

Posted - 2011-01-27 : 09:15:15
Hi all,

What I'm trying to achieve is a Windows Form Application that has various fields, which when submitted, posts the form data into an SQL database. I'd like to know how to do this, but to have a database built into the project, and when I've finished programming it, I'll be able to compile it into an installation file - an .msi. This way I can distribute the application as an enterprise/self-contained application.

I know the VB.NET part, but not how to create the correct instance of an SQL database. I'm guessing that if I create the database in Microsoft SQL Server Management Studio it won't be part of the compiled project. I believe that the correct way (with my intentions in mind) is to add an .mdf database to the project. If so, how do I connect to it? The database use won't be too heavy < 1,500 records

Any help or pointers will be greatly appreciated :)

Thanks

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-01-27 : 09:21:31
To attach the mdf file you will first have to install sql server - which unless you use the express version will need a licence.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-01-27 : 09:34:10
sql server seems like a heavy hammer if that's all the data you have.

not to snub sql server, but I would look at sqlite for this since your project is meant to be self contained (I assume that means no server): http://www.sqlite.org

there's a .net interface too: http://sqlite.phxsoftware.com


elsasoft.org
Go to Top of Page

nhighton
Starting Member

11 Posts

Posted - 2011-01-29 : 01:11:03
Thanks Jezemine I've never heard of SQL Lite before and it looks like what I need.

Having a play with it now. Will all workstations that use this application need SQL Lite installed on them, or will the compiled project have all the resources it will need?

Thanks
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-01-29 : 02:43:45
each app will need it, but it's just a dll. note that this is NOT a good solution if you need a server. will all the users of your app need to see the same data? or does each user get their own copy of the db? only in the latter case is sqlite an option.


elsasoft.org
Go to Top of Page

nhighton
Starting Member

11 Posts

Posted - 2011-01-29 : 05:46:23
Only 1 person will be using it, no need for multiple users or it being hosted. It's going well so far apart from 1 error which I can't seem to get past.

I experimented earlier with inserting the contents of a textbox into my SQL lite database, it worked fine. Now I've come to doing it with multiple textboxes under 1 button click procedure, it's not playing ball. I'm getting this (a apparently very generic error):

Value cannot be null.
Parameter name: s

My code is as follows:


Public Class frmMaterials

Dim SQLStr As String
Private ConnString As String
Private strSLA As String

Private Sub chkYes_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkYes.CheckedChanged

' If the Yes checkbox state changes: If Yes is checked, No is invisible. If Yes is unchecked, No is visible.
If chkYes.Checked = True Then
chkNo.Visible = False
End If

If chkYes.Checked = False Then
chkNo.Visible = True
End If

' Make 'SLA Included?' a string and set the Yes/No value according to which checkbox is checked.
If chkYes.Checked Then
strSLA = "Yes"
ElseIf chkNo.Checked Then
strSLA = "No"
End If

End Sub


Private Sub chkNo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkNo.CheckedChanged

' If the No checkbox state changes: If No is checked, Yes is invisible. If No is unchecked, Yes is visible.
If chkNo.Checked = True Then
chkYes.Visible = False
End If

If chkNo.Checked = False Then
chkYes.Visible = True
End If

' Make 'SLA Included?' a string and set the Yes/No value according to which checkbox is checked.
If chkYes.Checked Then
strSLA = "Yes"
ElseIf chkNo.Checked Then
strSLA = "No"
End If

End Sub

Private Sub txtUnitCost_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUnitCost.TextChanged

' When the Unit Cost field changes; If the value of the Quantity field is more than -1, then Total Cost = Quantity * Cost
If txtQuantity.Text <> -1 Then
txtTotalCost.Text = CDbl(txtQuantity.Text) * CDbl(txtUnitCost.Text)
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

' Set connection string
ConnString = "data source=C:\Users\xxxx.Number2\Documents\Visual Studio 2010\Projects\BCSC\BCSC\Materials;page size=1500;cache size=2300;Password=xxxxxxxx"

' Set connection variables: Connection and Command properties
Dim SQLConn As New SQLite.SQLiteConnection()
Dim SQLCmd As New SQLite.SQLiteCommand()
SQLConn.ConnectionString = ConnString
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = SQLStr



' Set Textbox properties to strings
Dim strCustomer As String = txtCustomer.Text
Dim strDescription As String = txtCustomer.Text
Dim strMake As String = txtMake.Text
Dim strModel As String = txtModel.Text
Dim strSupplier As String = txtSupplier.Text
Dim strMOQ As String = txtMOQ.Text
Dim strQuantity As String = txtQuantity.Text
Dim strUnitcost As String = txtUnitCost.Text
Dim strTotalcost As String = txtTotalCost.Text

'Set the SQL command
SQLStr = "INSERT INTO materials (Customer, SLAIncluded, Description, Make, Model, Supplier, MOQ, Quantity, UnitCost, TotalCost) VALUES ('" & strCustomer & "', '" & strSLA & "', '" & strDescription & "', '" & strMake & "', '" & strModel & "', '" & strSupplier & "', " & CDbl(strMOQ) & ", " & CDbl(strQuantity) & ", " & CDbl(strUnitcost) & ", " & CDbl(strTotalcost) & ")"

' Open the connection to the SQL Lite database and store record
SQLConn.Open()

SQLCmd.ExecuteNonQuery()

SQLConn.Close()


End Sub

End Class



The part commented as ' Set the SQL command is likely where there error is from. But when I submit the form, the error is thrown from the
SQLCmd.ExecuteNonQuery()
line.

Any help would be massively appreciated!

Thanks
Go to Top of Page

nhighton
Starting Member

11 Posts

Posted - 2011-01-29 : 06:41:16
The SQL query was golden, but I've solved it now. I don't think it liked the way the declaring of variables was ordered, so worked fine with a bit of a shuffle around. Funny that although Microsoft include Try, Catch statements in their products, they rarely use them themselves as it seems about 80% of visual studio-related problems seem to be covered by about 8 error messages.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-01-29 : 18:50:14
ok cool. if you have further questions about sqlite probably better post elsewhere though. this forum is for sql server only.

also you should edit your post above to remove passwords in the connection string!


elsasoft.org
Go to Top of Page

nhighton
Starting Member

11 Posts

Posted - 2011-01-30 : 01:25:25
Heh probably a good idea,

thanks for your help
Go to Top of Page
   

- Advertisement -