-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditCarForm.vb
194 lines (173 loc) · 7.88 KB
/
EditCarForm.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Imports System.Data.SqlClient
Public Class EditCarForm
Private Sub EditCarForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim id As Integer = MainForm.GlobalVariables.SelectedId
Dim s As String = "karol"
'
's = GetDatbaseColumn(id, "brand")
' Console.WriteLine("brand = " + s)
GetCarBrandList()
ComboBoxMarka.SelectedItem = MainForm.GlobalVariables.RowBrand
GetCarModelList(ComboBoxMarka.SelectedItem)
ComboBoxModel.SelectedItem = MainForm.GlobalVariables.RowModel
ComboBoxKolor.SelectedItem = MainForm.GlobalVariables.RowColor
TextBoxRocznik.Text = MainForm.GlobalVariables.RowGeneration
TextBoxZdjecie.Text = MainForm.GlobalVariables.RowZdjecie
If MainForm.GlobalVariables.RowKlima = "1" Then
CheckBoxKlimatyzacja.Checked = True
End If
If MainForm.GlobalVariables.RowMetalik = "1" Then
CheckBoxLakierMetalik.Checked = True
End If
If MainForm.GlobalVariables.RowPark = "1" Then
CheckBoxCzujniki.Checked = True
End If
If MainForm.GlobalVariables.RowAbs = "1" Then
CheckBoxABS.Checked = True
End If
If MainForm.GlobalVariables.RowEsp = "1" Then
CheckBoxESP.Checked = True
End If
If MainForm.GlobalVariables.RowKeyless = "1" Then
CheckBoxKeyless.Checked = True
End If
'GetSelectedCar(MainForm.GlobalVariables.SelectedId)
End Sub
Function GetCarBrandList() As Integer
'Dim queryString As String = "SELECT * FROM dbo.CarBrand;"
Dim queryString As String = "SELECT * FROM dbo.CarsBrandList ORDER BY Id;"
Dim reader As System.Data.SqlClient.SqlDataReader
'queryString = String.Format(msg, TextBoxLogin.Text)
Using connection As New SqlConnection(MainForm.GlobalVariables.DatabaseConStr)
Dim row_id As Integer
Dim row_name As String
Dim command As New SqlCommand(queryString, connection)
Dim result As Boolean
Dim error_cnt As Integer = 0
Try
command.Connection.Open()
result = True
Catch ex As Exception
MessageBox.Show("Nie można połączyć się z bazą danych w celu odczytania listy producentów")
result = False
End Try
If result = True Then
Try
reader = command.ExecuteReader()
'ComboBoxMarka.Items.Clear()
While reader.Read()
'Console.WriteLine(reader.FieldCount.ToString())
row_id = reader(0)
row_name = reader(1)
If row_id > 0 And row_name IsNot String.Empty Then
ComboBoxMarka.Items.Insert(row_id - 1, row_name)
'Console.WriteLine(row_id.ToString() + "." + row_name.ToString)
Else
Console.WriteLine("Empty data !")
error_cnt += 1
End If
End While
Catch ex As Exception
MessageBox.Show("Nie udało sie odczytac listy producentow")
error_cnt += 1
End Try
'If error_cnt = 0 Then
'tutaj odswiez tabele
'End If
End If
command.Connection.Close()
command.Connection.Dispose()
End Using
Return 1
End Function
Function GetCarModelList(ByVal brand_name As String)
'Dim msg As String = "SELECT * FROM dbo.CarModels WHERE brand_id = {0};"
Dim msg As String = "SELECT * FROM dbo.CarsModelList WHERE BrandName = '{0}' ORDER BY Name;"
'Dim msg As String = "SELECT Id, model FROM dbo.CarsNewTable WHERE brand = '{0}';"
Dim reader As System.Data.SqlClient.SqlDataReader
Dim queryString As String
queryString = String.Format(msg, brand_name)
Using connection As New SqlConnection(MainForm.GlobalVariables.DatabaseConStr)
Dim row_id As Integer = 0
Dim row_name As String
Dim command As New SqlCommand(queryString, connection)
'Dim command As New SqlCommand(msg, connection)
Dim result As Boolean
Try
command.Connection.Open()
result = True
Catch ex As Exception
MessageBox.Show("Nie można połączyć się z bazą danych")
result = False
End Try
If result = True Then
Try
reader = command.ExecuteReader()
ComboBoxModel.Items.Clear()
While reader.Read()
'Console.WriteLine(reader.FieldCount.ToString())
row_name = reader(1)
If row_name IsNot String.Empty Then
ComboBoxModel.Items.Insert(row_id, row_name)
row_id += 1
Console.WriteLine(row_id.ToString() + "." + row_name.ToString)
Else
Console.WriteLine("Empty data !")
End If
End While
Catch ex As Exception
MessageBox.Show("Nie udało sie odczytac listy modeli")
End Try
Else
Console.WriteLine("Nie udane połączenie z bazą")
End If
command.Connection.Close()
command.Connection.Dispose()
End Using
End Function
Private Sub ButtonZapisz_Click(sender As Object, e As EventArgs) Handles ButtonZapisz.Click
Dim UpdateQuery As String = "UPDATE dbo.CarsDatabase SET brand='{0}', model='{1}', generation='{2}', color='{3}', metalic={4}, ac={5}, abs={6}, esp={7}, park={8}, keyless={9}, image='{10}' WHERE Id = {11};"
Dim id As Integer = MainForm.DataGridViewPojazdy.Rows(MainForm.CarsDatabaseBindingSource.Position).Cells(0).Value
Dim metalic_val As Integer = 0
Dim klima_val As Integer = 0
Dim abs_val As Integer = 0
Dim esp_val As Integer = 0
Dim park_val As Integer = 0
Dim keyless_val As Integer = 0
If CheckBoxLakierMetalik.Checked = True Then
metalic_val = 1
End If
If CheckBoxKlimatyzacja.Checked = True Then
klima_val = 1
End If
If CheckBoxABS.Checked = True Then
abs_val = 1
End If
If CheckBoxESP.Checked = True Then
esp_val = 1
End If
If CheckBoxCzujniki.Checked = True Then
park_val = 1
End If
If CheckBoxKeyless.Checked = True Then
keyless_val = 1
End If
UpdateQuery = String.Format(UpdateQuery, ComboBoxMarka.SelectedItem, ComboBoxModel.SelectedItem, TextBoxRocznik.Text, ComboBoxKolor.SelectedItem, metalic_val, klima_val, abs_val, esp_val, park_val, keyless_val, TextBoxZdjecie.Text, id)
Console.WriteLine(UpdateQuery)
Try
FormLogowanie.CreateCommand(UpdateQuery, MainForm.GlobalVariables.DatabaseConStr)
MessageBox.Show("Dane pojazdu zostały zaktualizowane", "Potwierdzenie", MessageBoxButtons.OK)
Catch ex As Exception
MessageBox.Show("Wystąpił błąd. Dane nie zostały zaktualizowane", "Blad", MessageBoxButtons.OK)
End Try
MainForm.SzukajPojazdow()
Me.Close()
End Sub
Private Sub ComboBoxMarka_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxMarka.SelectedIndexChanged
ComboBoxModel.Items.Clear()
GetCarModelList(ComboBoxMarka.SelectedItem)
End Sub
Private Sub ButtonAnuluj_Click(sender As Object, e As EventArgs) Handles ButtonAnuluj.Click
Me.Close()
End Sub
End Class