Mysql Error - Update Query - mysql

do you find any issue in these below MySQL query. Actually this Insert query is not working. the tools picks the values from FORM controls correctly but it is not updating.
the values ain't update in MySQL DB. the coding brings the form values correctly but it is not updating in MySQL DB
actually the query where i am using in VB.Net (VS2010)
Private Sub btnupdate_Click(sender As System.Object, e As System.EventArgs) Handles btnupdate.Click
Dim ch_date As Date = dtpdate.Value
If ch_date = getToday(Now) Then
checkdate(ch_date)
Else
checkdate(ch_date)
End If
cleanfields()
End Sub
Function checkdate(ByVal chdate As Date)
Dim tmpreader As MySqlDataReader
Dim usr As String
'Dim dtp_date As Date = dtpdate.Value
usr = getUserName()
usr = getFullName(getUserName())
Try
If main_form.mySQLconn1.State = ConnectionState.Closed Then
main_form.mySQLconn1.Open()
End If
main_form.cmdproductivity.CommandText = "SELECT * FROM productivity WHERE productivity_date = #productivity_date"
main_form.cmdproductivity.Parameters("#productivity_date").Value = chdate
tmpreader = main_form.cmdproductivity.ExecuteReader()
If tmpreader.HasRows Then
main_form.cmdproductivity.CommandText = "UPDATE productivity SET agent = #agent, productivity_date = #productivity_date, actual_date = #actual_date, total_count = #total_count, eq_pr = #eq_pr, c_rebook=#c_rebook, c_nonwatson=#c_nonwatson, c_coa=#c_coa, c_watson=#c_watson, c_velocity=#c_velocity, c_quote=#c_quote, c_crd=#c_crd, c_eclaims=#c_eclaims, c_ecalls=#c_ecalls, c_xi=#c_xi, c_brazil=#c_brazil, c_cancel=#c_cancel, c_queries=#c_queries, c_partrebook=#c_partrebook, c_nci=#c_nci, c_phil=#c_phil, c_qc=#c_qc, c_watsonqc=#c_watsonqc, c_monitor=#c_monitor, c_enduser=#c_enduser, c_idoc=#c_idoc, c_servercancel=#c_servercancel, eq_rebook=#eq_rebook, eq_nonwatson=#eq_nonwatson, eq_coa=#eq_coa, eq_watson=#eq_watson, eq_velocity=#eq_velocity, eq_quote=#eq_quote, eq_crd=#eq_crd, eq_eclaims=#eq_eclaims, eq_ecalls=#eq_ecalls, eq_xi=#eq_xi, eq_brazil=#eq_brazil, eq_cancel=#eq_cancel, eq_queries=#eq_queries, eq_partrebook=#eq_partrebook, eq_nci=#eq_nci, eq_phil=#eq_phil, eq_qc=#eq_qc, eq_watsonqc=#eq_watsonqc, eq_monitor=#eq_monitor, eq_enduser=#eq_enduser, eq_idoc=#eq_idoc, eq_servercancel=#eq_servercancel WHERE productivity_date = #productivity_date"
main_form.cmdproductivity.Parameters("#agent").Value = Trim(usr)
main_form.cmdproductivity.Parameters("#productivity_date").Value = Trim(chdate)
main_form.cmdproductivity.Parameters("#actual_date").Value = getToday(Now)
main_form.cmdproductivity.Parameters("#total_count").Value = Trim(txtcount.Text)
main_form.cmdproductivity.Parameters("#eq_pr").Value = Trim(txtEQPRs.Text)
main_form.cmdproductivity.Parameters("#c_rebook").Value = Trim(txtrebook.Text)
main_form.cmdproductivity.Parameters("#c_nonwatson").Value = Trim(txtnonwatson.Text)
main_form.cmdproductivity.Parameters("#c_coa").Value = Trim(txtcoa.Text)
main_form.cmdproductivity.Parameters("#c_watson").Value = Trim(txtwatson.Text)
main_form.cmdproductivity.Parameters("#c_velocity").Value = Trim(txtvelocity.Text)
main_form.cmdproductivity.Parameters("#c_quote").Value = Trim(txtquote.Text)
main_form.cmdproductivity.Parameters("#c_crd").Value = Trim(txtcrd.Text)
main_form.cmdproductivity.Parameters("#c_eclaims").Value = Trim(txteclaims.Text)
main_form.cmdproductivity.Parameters("#c_ecalls").Value = Trim(txtecalls.Text)
main_form.cmdproductivity.Parameters("#c_xi").Value = Trim(txtxi.Text)
main_form.cmdproductivity.Parameters("#c_brazil").Value = Trim(txtbrazil.Text)
main_form.cmdproductivity.Parameters("#c_cancel").Value = Trim(txtcancel.Text)
main_form.cmdproductivity.Parameters("#c_queries").Value = Trim(txtqueries.Text)
main_form.cmdproductivity.Parameters("#c_partrebook").Value = Trim(txtpartrebook.Text)
main_form.cmdproductivity.Parameters("#c_nci").Value = Trim(txtnci.Text)
main_form.cmdproductivity.Parameters("#c_phil").Value = Trim(txtphil.Text)
main_form.cmdproductivity.Parameters("#c_qc").Value = Trim(txtqc.Text)
main_form.cmdproductivity.Parameters("#c_watsonqc").Value = Trim(txtwatsonqc.Text)
main_form.cmdproductivity.Parameters("#c_monitor").Value = Trim(txtmonitor.Text)
main_form.cmdproductivity.Parameters("#c_enduser").Value = Trim(txtenduser.Text)
main_form.cmdproductivity.Parameters("#c_idoc").Value = Trim(txtidoc.Text)
main_form.cmdproductivity.Parameters("#c_servercancel").Value = Trim(txtservercancel.Text)
main_form.cmdproductivity.Parameters("#eq_rebook").Value = Trim(txtEQrebook.Text)
main_form.cmdproductivity.Parameters("#eq_nonwatson").Value = Trim(txtEQnonwatson.Text)
main_form.cmdproductivity.Parameters("#eq_coa").Value = Trim(txtEQcoa.Text)
main_form.cmdproductivity.Parameters("#eq_watson").Value = Trim(txtEQwatson.Text)
main_form.cmdproductivity.Parameters("#eq_velocity").Value = Trim(txtEQvelocity.Text)
main_form.cmdproductivity.Parameters("#eq_quote").Value = Trim(txtEQquote.Text)
main_form.cmdproductivity.Parameters("#eq_crd").Value = Trim(txtEQcrd.Text)
main_form.cmdproductivity.Parameters("#eq_eclaims").Value = Trim(txtEQeclaims.Text)
main_form.cmdproductivity.Parameters("#eq_ecalls").Value = Trim(txtEQecalls.Text)
main_form.cmdproductivity.Parameters("#eq_xi").Value = Trim(txtEQxi.Text)
main_form.cmdproductivity.Parameters("#eq_brazil").Value = Trim(txtEQbrazil.Text)
main_form.cmdproductivity.Parameters("#eq_cancel").Value = Trim(txtEQcancel.Text)
main_form.cmdproductivity.Parameters("#eq_queries").Value = Trim(txtEQqueries.Text)
main_form.cmdproductivity.Parameters("#eq_partrebook").Value = Trim(txtEQpartrebook.Text)
main_form.cmdproductivity.Parameters("#eq_nci").Value = Trim(txtEQnci.Text)
main_form.cmdproductivity.Parameters("#eq_phil").Value = Trim(txtEQphil.Text)
main_form.cmdproductivity.Parameters("#eq_qc").Value = Trim(txtEQqc.Text)
main_form.cmdproductivity.Parameters("#eq_watsonqc").Value = Trim(txtEQwatsonqc.Text)
main_form.cmdproductivity.Parameters("#eq_monitor").Value = Trim(txtEQmonitor.Text)
main_form.cmdproductivity.Parameters("#eq_enduser").Value = Trim(txtEQenduser.Text)
main_form.cmdproductivity.Parameters("#eq_idoc").Value = Trim(txtEQidoc.Text)
main_form.cmdproductivity.Parameters("#eq_servercancel").Value = Trim(txtEQservercancel.Text)
Else
'main_form.cmdproductivity.CommandText = "INSERT INTO productivity " _
' & "(agent,productivity_date,actual_date,total_count,eq_pr,c_rebook,c_nonwatson,c_coa,c_watson,c_velocity,c_quote,c_crd,c_eclaims,c_ecalls,c_xi,c_brazil,c_cancel,c_queries,c_partrebook,c_nci,c_phil,c_qc,c_watsonqc,c_monitor,c_enduser,c_idoc,c_servercancel,eq_rebook,eq_nonwatson,eq_coa,eq_watson,eq_velocity,eq_quote,eq_crd,eq_eclaims,eq_ecallseq_xi,eq_brazil,eq_cancel,eq_queries,eq_partrebook,eq_nci,eq_phil,eq_qc,eq_watsonqc,eq_monitor,eq_enduser,eq_idoc,eq_servercancel VALUES" _
' & "(#agent,#productivity_date,#actual_date,#total_count,#eq_pr,#c_rebook,#c_nonwatson,#c_coa,#c_watson,#c_velocity,#c_quote,#c_crd,#c_eclaims,#c_ecalls,#c_xi,#c_brazil,#c_cancel,#c_queries,#c_partrebook,#c_nci,#c_phil,#c_qc,#c_watsonqc,#c_monitor,#c_enduser,#c_idoc,#c_servercancel,#eq_rebook,#eq_nonwatson,#eq_coa,#eq_watson,#eq_velocity,#eq_quote,#eq_crd,#eq_eclaims,#eq_ecalls,#eq_xi,#eq_brazil,#eq_cancel,#eq_queries,#eq_partrebook,#eq_nci,#eq_phil,#eq_qc,#eq_watsonqc,#eq_monitor,#eq_enduser,#eq_idoc,#eq_servercancel)"
main_form.cmdproductivity.CommandText = "INSERT INTO productivity(agent, productivity_date, actual_date, total_count, eq_pr, c_rebook, c_nonwatson, c_coa, c_watson, c_velocity, c_quote, c_crd, c_eclaims, c_ecalls, c_xi, c_brazil, c_cancel, c_queries, c_partrebook, c_nci, c_phil, c_qc, c_watsonqc, c_monitor, c_enduser, c_idoc, c_servercancel, eq_rebook, eq_nonwatson, eq_coa, eq_watson, eq_velocity, eq_quote, eq_crd, eq_eclaims, eq_ecalls, eq_xi, eq_brazil, eq_cancel, eq_queries, eq_partrebook, eq_nci, eq_phil, eq_qc, eq_watsonqc, eq_monitor, eq_enduser, eq_idoc, eq_servercancel) VALUES (#agent,#productivity_date,#actual_date,#total_count,#eq_pr,#c_rebook,#c_nonwatson,#c_coa,#c_watson,#c_velocity,#c_quote,#c_crd,#c_eclaims,#c_ecalls,#c_xi,#c_brazil,#c_cancel,#c_queries,#c_partrebook,#c_nci,#c_phil,#c_qc,#c_watsonqc,#c_monitor,#c_enduser,#c_idoc,#c_servercancel,#eq_rebook,#eq_nonwatson,#eq_coa,#eq_watson,#eq_velocity,#eq_quote,#eq_crd,#eq_eclaims,#eq_ecalls,#eq_xi,#eq_brazil,#eq_cancel,#eq_queries,#eq_partrebook,#eq_nci,#eq_phil,#eq_qc,#eq_watsonqc,#eq_monitor,#eq_enduser,#eq_idoc,#eq_servercancel)"
main_form.cmdproductivity.Parameters("#agent").Value = Trim(usr)
main_form.cmdproductivity.Parameters("#productivity_date").Value = Trim(chdate)
main_form.cmdproductivity.Parameters("#actual_date").Value = getToday(Now)
main_form.cmdproductivity.Parameters("#total_count").Value = Trim(txtcount.Text)
main_form.cmdproductivity.Parameters("#eq_pr").Value = Trim(txtEQPRs.Text)
main_form.cmdproductivity.Parameters("#c_rebook").Value = Trim(txtrebook.Text)
main_form.cmdproductivity.Parameters("#c_nonwatson").Value = Trim(txtnonwatson.Text)
main_form.cmdproductivity.Parameters("#c_coa").Value = Trim(txtcoa.Text)
main_form.cmdproductivity.Parameters("#c_watson").Value = Trim(txtwatson.Text)
main_form.cmdproductivity.Parameters("#c_velocity").Value = Trim(txtvelocity.Text)
main_form.cmdproductivity.Parameters("#c_quote").Value = Trim(txtquote.Text)
main_form.cmdproductivity.Parameters("#c_crd").Value = Trim(txtcrd.Text)
main_form.cmdproductivity.Parameters("#c_eclaims").Value = Trim(txteclaims.Text)
main_form.cmdproductivity.Parameters("#c_ecalls").Value = Trim(txtecalls.Text)
main_form.cmdproductivity.Parameters("#c_xi").Value = Trim(txtxi.Text)
main_form.cmdproductivity.Parameters("#c_brazil").Value = Trim(txtbrazil.Text)
main_form.cmdproductivity.Parameters("#c_cancel").Value = Trim(txtcancel.Text)
main_form.cmdproductivity.Parameters("#c_queries").Value = Trim(txtqueries.Text)
main_form.cmdproductivity.Parameters("#c_partrebook").Value = Trim(txtpartrebook.Text)
main_form.cmdproductivity.Parameters("#c_nci").Value = Trim(txtnci.Text)
main_form.cmdproductivity.Parameters("#c_phil").Value = Trim(txtphil.Text)
main_form.cmdproductivity.Parameters("#c_qc").Value = Trim(txtqc.Text)
main_form.cmdproductivity.Parameters("#c_watsonqc").Value = Trim(txtwatsonqc.Text)
main_form.cmdproductivity.Parameters("#c_monitor").Value = Trim(txtmonitor.Text)
main_form.cmdproductivity.Parameters("#c_enduser").Value = Trim(txtenduser.Text)
main_form.cmdproductivity.Parameters("#c_idoc").Value = Trim(txtidoc.Text)
main_form.cmdproductivity.Parameters("#c_servercancel").Value = Trim(txtservercancel.Text)
main_form.cmdproductivity.Parameters("#eq_rebook").Value = Trim(txtEQrebook.Text)
main_form.cmdproductivity.Parameters("#eq_nonwatson").Value = Trim(txtEQnonwatson.Text)
main_form.cmdproductivity.Parameters("#eq_coa").Value = Trim(txtEQcoa.Text)
main_form.cmdproductivity.Parameters("#eq_watson").Value = Trim(txtEQwatson.Text)
main_form.cmdproductivity.Parameters("#eq_velocity").Value = Trim(txtEQvelocity.Text)
main_form.cmdproductivity.Parameters("#eq_quote").Value = Trim(txtEQquote.Text)
main_form.cmdproductivity.Parameters("#eq_crd").Value = Trim(txtEQcrd.Text)
main_form.cmdproductivity.Parameters("#eq_eclaims").Value = Trim(txtEQeclaims.Text)
main_form.cmdproductivity.Parameters("#eq_ecalls").Value = Trim(txtEQecalls.Text)
main_form.cmdproductivity.Parameters("#eq_xi").Value = Trim(txtEQxi.Text)
main_form.cmdproductivity.Parameters("#eq_brazil").Value = Trim(txtEQbrazil.Text)
main_form.cmdproductivity.Parameters("#eq_cancel").Value = Trim(txtEQcancel.Text)
main_form.cmdproductivity.Parameters("#eq_queries").Value = Trim(txtEQqueries.Text)
main_form.cmdproductivity.Parameters("#eq_partrebook").Value = Trim(txtEQpartrebook.Text)
main_form.cmdproductivity.Parameters("#eq_nci").Value = Trim(txtEQnci.Text)
main_form.cmdproductivity.Parameters("#eq_phil").Value = Trim(txtEQphil.Text)
main_form.cmdproductivity.Parameters("#eq_qc").Value = Trim(txtEQqc.Text)
main_form.cmdproductivity.Parameters("#eq_watsonqc").Value = Trim(txtEQwatsonqc.Text)
main_form.cmdproductivity.Parameters("#eq_monitor").Value = Trim(txtEQmonitor.Text)
main_form.cmdproductivity.Parameters("#eq_enduser").Value = Trim(txtEQenduser.Text)
main_form.cmdproductivity.Parameters("#eq_idoc").Value = Trim(txtEQidoc.Text)
main_form.cmdproductivity.Parameters("#eq_servercancel").Value = Trim(txtEQservercancel.Text)
End If
Catch ex As Exception
MsgBox("Database Error: " & ex.ToString() & vbCrLf & "The error have been logged .... ", MsgBoxStyle.OkOnly, main_form.dbErrTitle)
writeLogs("Database Error: " & ex.ToString())
Finally
main_form.mySQLconn1.Close()
End Try
Return True
End Function

I don't calculate your column number, but do you miss one column at end? where is the last id which exists in column but not in VALUSE part:
(eq_servercancel, id) vs
(#eq_servercancel)

It would really help if you edit your answer with the error you are getting, but at a glance:
You are adding "id" to you column list in the INSERT statement, but you do not have a "id" value in your VALUES statement. So either remove "id" from the INSERT statement or send through a value for "id".

Related

Datagrid showing 1 result only

I have created a code to display data from datagrid and I have 3 data on the database but the query only display 1 result. here is my code below:
Public Sub displayGrid()
Using connection As New MySqlConnection(connectionstring)
SQL = "Select * from IPQC_modelParameter"
Using da As New MySqlDataAdapter(SQL, connection)
Dim ds = New DataSet
da.Fill(ds)
For x As Integer = 0 To ds.Tables(0).Rows.Count
DataGridView1.Rows.Insert(0) 'this will add a new record on top, always
DataGridView1.Rows(0).Cells(0).Value = ds.Tables(0).Rows(x)("id")
DataGridView1.Rows(0).Cells(1).Value = ds.Tables(0).Rows(x)("full_model")
DataGridView1.Rows(0).Cells(2).Value = ds.Tables(0).Rows(x)("model_name")
DataGridView1.Rows(0).Cells(3).Value = ds.Tables(0).Rows(x)("parameter")
DataGridView1.Rows(0).Cells(4).Value = ds.Tables(0).Rows(x)("instrument")
DataGridView1.Rows(0).Cells(5).Value = ds.Tables(0).Rows(x)("nominal")
DataGridView1.Rows(0).Cells(6).Value = ds.Tables(0).Rows(x)("tolerance")
DataGridView1.Rows(0).Cells(7).Value = ds.Tables(0).Rows(x)("ucl")
DataGridView1.Rows(0).Cells(8).Value = ds.Tables(0).Rows(x)("lcl")
'DataGridView1.Rows(0).Cells(9).Value = ds.Tables(0).Rows(x)("uot")
'DataGridView1.Rows(0).Cells(10).Value = ds.Tables(0).Rows(x)("lot")
DataGridView1.Rows(0).Cells(11).Value = ds.Tables(0).Rows(x)("mrnominal")
DataGridView1.Rows(0).Cells(12).Value = ds.Tables(0).Rows(x)("mrucl")
'DataGridView1.Rows(0).Cells(13).Value = ds.Tables(0).Rows(x)("status")
'DataGridView1.Rows(0).Cells(14).Value = ds.Tables(0).Rows(x)("mp_id")
DataGridView1.Rows(0).Cells(15).Value = ds.Tables(0).Rows(x)("usl")
DataGridView1.Rows(0).Cells(16).Value = ds.Tables(0).Rows(x)("lsl")
DataGridView1.Rows(0).Cells(17).Value = ds.Tables(0).Rows(x)("conversion")
Next
End Using
End Using
End Sub

about field update using function with case

I'm trying to update some field value depending two string field using a function with If and Case, function below:
Private Function checkDATI(tipotransazione As String, tipovendita As String) As String
Dim r As String
r = ""
If tipotransazione = "VENDITA" Then
Select Case tipovendita
Case "ARMI"
If Me.txtMatricola = "" Then r = "Matricola"
If Me.txtModello = "" Then r = "Modello"
If Me.txtCalibro = "" Then r = "Calibro"
If Me.txtTipoArma = "" Then r = "Tipo Arma"
If Me.txtFabrica = "" Then r = "Fabbrica"
Case "VENDITA ARMI/MUNIZIONI"
Case "MUNIZIONI"
End Select
End If
If tipotransazione = "ACQUISTO" Then
Select Case tipovendita
Case "ARMI"
If Me.txtMatricola = "" Then r = "Matricola"
If Me.txtModello = "" Then r = "Modello"
If Me.txtCalibro = "" Then r = "Calibro"
If Me.txtTipoArma = "" Then r = "Tipo Arma"
If Me.txtFabrica = "" Then r = "Fabbrica"
Case "VENDITA ARMI/MUNIZIONI"
Case "MUNIZIONI"
End Select
End If
checkDATI = r
End Function
And then when I call this function in a command button event as:
MsgBox (checkDATI(Me.CausaleMov, Me.txt_tipomov))
It's not updating that field. What is wrong?
When you say:
If Me.txtMatricola = "" Then r = "Matricola"
What you are doing is assigning the value: "Matricola" to the variable:r. Instead what I think you want is this:
If Me.txtMatricola = "" Then Me.txtMatricola = "Matricola"
This actually sets the value of the field: txtMatricola to "Matricola". This goes for all of the fields that are in your Case statements.

index is outside the bounds of array

My try/catch expression always gives me a error message that "index is outside the bounds of array" however the function is working perfectly the way it should work. The only problem is that annoying mistake.
this is the code I'm using:
Try
conn.Open()
Dim Qwery As String = "SELECT a.accid AS 'No', a.setID AS 'ID', l.class AS 'Type', CONCAT(fname,' ',middname,' ',lname ) AS 'Name', date(a.bdate) AS 'BirthDate', l.mail AS 'Acount Name', l.login AS 'Login', l.gender AS 'Gender', l.position AS 'Position', l.department AS 'Department', l.tel AS 'Tel No', l.localno AS 'Office Tel', l.cellno AS 'Cell Phone', l.alt_email AS 'Other Email', l.classif AS 'Classification', l.status AS 'Status' FROM entry a, liist l WHERE a.accid = l.accid AND l.status = '0' ORDER BY a.accid Desc"
Dim smd As MySqlCommand
Dim myreader As MySqlDataReader
smd = New MySqlCommand(Qwery, myconn)
myreader = smd.ExecuteReader
If myreader.Read() = True Then
IdBox.Text = myreader.GetValue(0)
IdNumBox.Text = myreader.GetValue(1)
TypeBox.Text = myreader.GetValue(2)
NameBox.Text = myreader.GetValue(3)
BDayBox.Text = myreader.GetValue(4)
AcNameBox.Text = myreader.GetValue(5)
PassBox.Text = myreader.GetValue(6)
GenBox.Text = myreader.GetValue(7)
PositionBox.Text = myreader.GetValue(8)
DepartmentBox.Text = myreader.GetValue(9)
TelBox.Text = myreader.GetValue(10)
OfficeBox.Text = myreader.GetValue(11)
CellBox.Text = myreader.GetValue(12)
altMailBox.Text = myreader.GetValue(13)
ClassBox.Text = myreader.GetValue(14)
StatusBox.Text = myreader.GetValue(15)
End If
conn.Close()
Catch ex As Exception
MsgBox("Error: " & ex.Message.ToString())
conn.Close()
End Try
The interesting point that I tried removing assignments of labels one by one. And up to myreader.GetValue(3) the error does not appear. If I try assigning more lables it appears again.
Does anyone know the reason behind?
Check the condition for the datareader has greater than 15 rows.then access your code .
myreader = smd.ExecuteReader
If myreader.Read() = True Then
if myreader.GetSchemaTable().Rows.Count >= 15 then
IdBox.Text = myreader.GetValue(0)
IdNumBox.Text = myreader.GetValue(1)
TypeBox.Text = myreader.GetValue(2)
NameBox.Text = myreader.GetValue(3)
BDayBox.Text = myreader.GetValue(4)
AcNameBox.Text = myreader.GetValue(5)
PassBox.Text = myreader.GetValue(6)
GenBox.Text = myreader.GetValue(7)
PositionBox.Text = myreader.GetValue(8)
DepartmentBox.Text = myreader.GetValue(9)
TelBox.Text = myreader.GetValue(10)
OfficeBox.Text = myreader.GetValue(11)
CellBox.Text = myreader.GetValue(12)
altMailBox.Text = myreader.GetValue(13)
ClassBox.Text = myreader.GetValue(14)
StatusBox.Text = myreader.GetValue(15)
End If
End If
or try datareaderObject.RecordsAffected >= 15
i think in your query date(a.bdate) AS 'BirthDate' is throwing the error
may be the reason is that a.bdate is null value and can not be converted into date..

Passing parameters to IdbCommand for mySQL database

I am trying to do something relatively simple using IdbCommand to execute an insert query.
Here's the code:
using (IDbConnection conn = DbHelper.GetConnection(DbConnString))
using (IDbCommand com = conn.CreateCommand())
{
com.CommandType = CommandType.Text;
com.CommandText =
String.Format(
"INSERT INTO {0} (`Date`, User, Type, `Comment`) VALUES (#Date, #User, #Type, #Comment);",
TableName);
conn.Open();
var parameterDate = com.CreateParameter();
parameterDate.ParameterName = "#Date";
parameterDate.Value = entry.Date;
parameterDate.DbType = DbType.DateTime;
com.Parameters.Add(parameterDate);
var parameterUser = com.CreateParameter();
parameterUser.ParameterName = "#User";
parameterUser.Value = entry.User;
parameterUser.DbType = DbType.String;
com.Parameters.Add(parameterUser);
var parameterLogType = com.CreateParameter();
parameterLogType.ParameterName = "#Type";
parameterLogType.Value = entry.Type;
parameterLogType.DbType = DbType.Int32;
com.Parameters.Add(parameterLogType);
var parameterComment = com.CreateParameter();
parameterComment.ParameterName = "#Comment";
parameterComment.Value = entry.Comment;
parameterComment.DbType = DbType.String;
com.Parameters.Add(parameterComment);
com.ExecuteNonQuery();
But I keep getting a MySqlException with the message "Column 'Date' cannot be null".
All my selects work fine, it's just this insert that has a problem and I can't see an obvious problem with it.
The parameter is populated with a valid DateTime during runtime.
I thought it might be related to the fact that Date is a reserved word and needs backquotes, but that's what online tutorials recommend.
Any ideas?
Found it!
For some reason instead of #, it needs ?
So the working code is:
using (IDbConnection conn = DbHelper.GetConnection(DbConnString))
using (IDbCommand com = conn.CreateCommand())
{
com.CommandType = CommandType.Text;
com.CommandText =
String.Format(
"INSERT INTO {0} (`Date`, User, Type, `Comment`) VALUES (?Date, ?User, ?Type, ?Comment);",
TableName);
conn.Open();
var parameterDate = com.CreateParameter();
parameterDate.ParameterName = "?Date";
parameterDate.Value = entry.Date;
parameterDate.DbType = DbType.DateTime;
com.Parameters.Add(parameterDate);
var parameterUser = com.CreateParameter();
parameterUser.ParameterName = "?User";
parameterUser.Value = entry.User;
parameterUser.DbType = DbType.String;
com.Parameters.Add(parameterUser);
var parameterLogType = com.CreateParameter();
parameterLogType.ParameterName = "?Type";
parameterLogType.Value = entry.Type;
parameterLogType.DbType = DbType.Int32;
com.Parameters.Add(parameterLogType);
var parameterComment = com.CreateParameter();
parameterComment.ParameterName = "?Comment";
parameterComment.Value = entry.Comment;
parameterComment.DbType = DbType.String;
com.Parameters.Add(parameterComment);
com.ExecuteNonQuery();
}

Capture primary key value

I'm adding a record to a table using the following code:
Dim rs1 As DAO.Recordset
Set rs1 = CurrentDb.OpenRecordset("QUOTE-Run", dbOpenDynaset)
rs1.AddNew
rs1.Fields("[QuoteNumber]").value = [txtQuoteNumber].value
rs1.Fields("[LeadTime]").value = [txtLeadTime].value
rs1.Fields("[Qty]").value = [txtQty].value
rs1.Fields("[Title]").value = [txtTitle].value
rs1.Fields("[Date]").value = VBA.DateTime.Date
rs1.Fields("[Time]").value = VBA.DateTime.Time
rs1.Fields("[InitiatedBy]").value = Application.CurrentUser
rs1.Fields("[IncompleteProblemNotes]").value = [txtIncompleteProblemNotes].value
rs1.Fields("[CustomerNotes]").value = [txtCustomerNotes].value
rs1.Fields("[Memo]").value = [txtMemo].value
rs1.Fields("[Memo1]").value = [txtMemo1].value
rs1.Fields("[PrefferedQuoteRunSelect]").value = [txtPrefferedQuoteRunSelect].value
rs1.Fields("[CombinedRun]").value = [chkCombinedRun].value
rs1.Update
When creating the record there is an AutoNumber primary key field called RunID that gets a value. I need to know what that value is so I can use it later in the code. What is the easiest way of capturing that value?
The easiest way I can think of for you to get the value of RunID is declaring a variable and setting that variable equal to the field before updating the record. Something like this:
Dim rs1 As DAO.Recordset
Set rs1 = CurrentDb.OpenRecordset("QUOTE-Run", dbOpenDynaset)
Dim TempRunID As Long
rs1.AddNew
rs1.Fields("[QuoteNumber]").value = [txtQuoteNumber].value
rs1.Fields("[LeadTime]").value = [txtLeadTime].value
rs1.Fields("[Qty]").value = [txtQty].value
rs1.Fields("[Title]").value = [txtTitle].value
rs1.Fields("[Date]").value = VBA.DateTime.Date
rs1.Fields("[Time]").value = VBA.DateTime.Time
rs1.Fields("[InitiatedBy]").value = Application.CurrentUser
rs1.Fields("[IncompleteProblemNotes]").value = [txtIncompleteProblemNotes].value
rs1.Fields("[CustomerNotes]").value = [txtCustomerNotes].value
rs1.Fields("[Memo]").value = [txtMemo].value
rs1.Fields("[Memo1]").value = [txtMemo1].value
rs1.Fields("[PrefferedQuoteRunSelect]").value = [txtPrefferedQuoteRunSelect].value
rs1.Fields("[CombinedRun]").value = [chkCombinedRun].value
TempRunID = rs1.Fields("[RunID]").value
rs1.Update
You can run a second query that uses the function
SELECT LAST_INSERT_ID();