I HAVE iNSERT TO TABLE AG_MAS IN fOXPRO FOLLOW:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection connect = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=pl_prov.dbf");
try
{
connect.Open();
string strSQL = "insert into ag_mas";
strSQL += "(ag_code,ag_level,ag_group,ag_rcode,ag_status,";
strSQL += "ag_type,ag_gender,ag_lname,ag_fname,ag_add1,";
strSQL += "ag_add2,ag_add3,ag_add4,ag_class,ag_vouch,";
strSQL += "pl_prov,pl_zone,ag_jdate,ag_ddate,ag_dbrith,ag_bank ,";
strSQL += "ag_bankno,ag_telno,ag_faxno,ag_email,ag_ppay,";
strSQL += "ag_contno,ag_dcont,ag_ladate,ag_lpolno,ag_lendno,";
strSQL += "ag_comm,ag_maxcom,ag_orcomm,ag_om1com,ag_om2com,";
strSQL += "ag_om3com,ag_oy1com, ag_oy2com,ag_oy3com,ag_network)";
strSQL += "values (";
strSQL += "'" + textBox1.Text + "',";//ag_code
strSQL += "'" + textBox2.Text + "',";//ag_level
strSQL += "'" + textBox3.Text + "',";//ag_group
strSQL += "' ' ,"; //ag_rcode
strSQL += "'" + textBox4.Text + "',";//ag_status
strSQL += "'" + textBox22.Text + "',";//ag_type
strSQL += "'" + textBox5.Text + "',";//ag_gender
strSQL += "'" + textBox6.Text + "',";//ag_lname
strSQL += "'" + textBox7.Text + "',";//ag_fname
strSQL += "'" + textBox8.Text + "',";//ag_add1
strSQL += "'" + textBox9.Text + "',";//ag_add2
strSQL += "'" + comboBox1.Text + "',";//ag_add3
strSQL += "' ' ,"; //ag_add4
strSQL += "'" + textBox10.Text + "',";//ag_class
strSQL += "'" + textBox11.Text + "',";//ag_vouch
strSQL += "'" + comboBox1.SelectedValue + "',";//pl_prov
strSQL += "' ' ,"; //pl_zone
strSQL += "'" + textBox12.Text + "',"; //ag_jdate
strSQL += "'1987/10/10',"; //ag_ddate
strSQL += "'1987/10/10',"; //ag_dbirth
strSQL += "' ' ,"; //ag_bank
strSQL += "' ' ,"; //ag_bankno
strSQL += "' ' ,"; //ag_telno
strSQL += "' ' ,"; //ag_faxno
strSQL += "' ' ,"; //ag_email
strSQL += "'" + textBox13.Text + "',"; //ag_ppay
strSQL += "'" + textBox15.Text + "',";//ag_contno
strSQL += "'" + textBox14.Text + "',";//ag_dcont
strSQL += "'1987/10/10', "; //ag_ladate
strSQL += "' ' ,"; //ag_lpopno
strSQL += "' ' ,"; //ag_lendno
strSQL += "0,"; //ag_comm
strSQL += "0,"; //ag_maxcom
strSQL += "0,"; //ag_orcomm
strSQL += "0,"; //ag_om1com
strSQL += "0,"; //ag_om2com
strSQL += "0,"; //ag_om3comm
strSQL += "0,"; //ag_oy1com
strSQL += "0,"; //ag_oy2Xcom
strSQL += "0,"; //ag_oy3com
strSQL += "' ')"; //ag_network
OleDbCommand cmd = new OleDbCommand(strSQL, connect);
cmd.ExecuteNonQuery();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}
}
BUT I CAN'T INSERT. BECAUSE ERROR "DATA MISS MATCH" PLEASE HELP ME.
I agree with Frank on the issue with dates... It could also be trying to do a text into a numeric field (or any other such swapping).
However, a bigger issue might be if/when you get into using a true SQL database such as Oracle, SQL-Server, MySQL, etc... (not that I have anything against VFP and have been programming with Foxpro since '85) But doing SQL statements like you have, especially in any web-based system would leave you wide-open to SQL-injection attacks.
Its better to use parameterized query/insert/update/deletes.
A Data Type Mismatch error occurs when you try to insert an inappropriate data type into a field. For example, if you try to store a string into an integer field.
At first glance I think the format of your dates is the problem. Change "'1987/10/10'," to "{^1987-10-10},".
If changing the dates does not correct the problem, it would be helpful if you could show me the result of the strSQL variable and structure of the ag_mas table.
I agree with Frank in regards to a solution and I believe that DRapp has a valid point about how you are going about your insert statement. To add to DRapp’s suggestion, I’d like to point out that you could use LINQ to VFP to handle writing all your sql commands.
Related
Below is my query which is created in .Net code for insertion in oracle table. I found other related articles but they are different and not answering this. I was creating below query using a datatable.
`Public Sub UpdateOracleRecordset(dtTable As DataTable)
Dim sql As String = String.Empty
Dim i As Integer = 0
sql = "insert into " + dtTable.TableName + "("
For Each dc As DataColumn In dtTable.Columns
sql = sql + dc.ToString() + ","
Next
sql = sql.TrimEnd(",") + ")" + " select "
Dim rowVal As String = String.Empty
For Each dtRow As DataRow In dtTable.Rows
For Each dc As DataColumn In dtTable.Columns
rowVal = rowVal + dtRow(dc).ToString() + ","
Next
rowVal = rowVal.TrimEnd(",")
Next
sql = sql + rowVal
ExecuteSQL(sql)
End Sub`
"insert into CS_INV(LOANNO,CASENUMBER,INQ_TYPE,FUP_REASON,FUP_DATE,FUP_PROM,USERID,DATA_DAT,UNIT) values ( 5735985,103550709,399,58,9/24/2018 1:37:01 AM,9/25/2018 12:00:00 AM,Anurag,9/24/2018 1:37:08 AM,1 ) "
Wenfried is right and I used prepared statements which helped to resolve the issue
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim sql As String = String.Empty
Dim sqlValues As String = String.Empty
Dim i As Integer = 0
sql = "insert into " + dtTable.TableName + "("
For Each dc As DataColumn In dtTable.Columns
sql = sql + dc.ToString() + ","
sqlValues = sqlValues + ":" + dc.ToString() + ","
Next
sql = sql.TrimEnd(",") + ")" + " values ( " + sqlValues.TrimEnd(",") + ")"
Dim commandText = sql
Dim Command As New OracleCommand(sql, conn)
For Each dtRow As DataRow In dtTable.Rows
For Each dc As DataColumn In dtTable.Columns
Command.Parameters.Add(New OracleParameter(dc.ToString(), dtRow(dc)))
Next
Next
Command.ExecuteNonQuery()
conn.Close()
Hi I am having a problem storing image to mssql server I tried both Memory stream and BLOB as well and in both scinarios I get the same error "cannot insert the value null into image column msg 515 level 16 state 2 line 1", any suggestion is more than welcome Thanks.
Dim sqlstring = " begin tran;"
sqlstring &= " INSERT INTO tbl_customers (stnname, cardnum,
family_name, city, fam_mem_nu, id_num, mrkz_num) VALUES
('" & stn & "','" & cd & "', '" & fmnm & "','" & ct & "', '"
& fnum & "', '" & idn & "', '" & cntr & "')"
sqlstring &= "INSERT INTO tbl_customers(imag) SELECT * FROM
OPENROWSET(BULK N'c:\temp\tempimg', SINGLE_BLOB) imag ; "
sqlstring &= "commit tran;"
sql.CommandText = sqlstring
sql.Connection = conn
conn.Open()
Dim ms As New MemoryStream
img = CameraControl1.SnapshotSourceImage
img.Save("c:\temp\tempimg", Imaging.ImageFormat.Png)
'CameraControl1.SnapshotSourceImage.Save(ms, Imaging.ImageFormat.Bmp)
' PictureBox3.Image.Save(ms, PictureBox3.Image.RawFormat)
' ms.ToArray()
' Dim data As Byte() = ms.GetBuffer()
' Dim p As New SqlClient.SqlParameter("#img", SqlDbType.VarBinary)
' p.Value = data
' sql.Parameters.Add(p)
Dim x As Integer = sql.ExecuteNonQuery
From This question, its answer is almost my answer. But I am facing some sql query issue, I have the following statement in VB
Dim results As DataRow() = table.Select("A = 'foo' AND B = 'bar' AND C = 'baz'")
I want to place foo, bar and baz in variables and use that variables in above statements.
Dim Varfoo As String = "foo"
Dim Varbar As String = "bar"
Dim Varbaz As String = "baz"
I managed to get one variable in statement as
Dim results As DataRow() = table.Select("A = " + Varfoo)
But how to insert multiple sort expressions with variables?
Edit: I got it solved with the answer of vikas as following;
Dim results As DataRow() = table.Select("A = '" & Varfoo & "' And B = '" & Varbar & "' And C = '" & Varbaz & "'")
Have you tried
Dim results As DataRow() = table.Select("A = '" & Varfoo & "'")
Edited
For OR operation
Dim results As DataRow() = table.Select("A = '" & Varfoo & "' OR B = '" & Varbar & "' OR C = '" & Varbaz & "'")
For AND operation
Dim results As DataRow() = table.Select("A = '" & Varfoo & "' AND B = '" & Varbar & "' AND C = '" & Varbaz & "'")
Tried every combo I can think of: String.Empty, IsDBNull.
selectPONumber is passed to "ByVal selectPONumber() As String" from "selectPONumber: [' + JSON.stringify('') + ']"
The StackTrace point to the second line of this VB
VB
If Not IsDBNull(selectPONumber) Then
If selectPONumber.Length > 1 Then
qry = qry + "and ("
For u As Integer = 0 To selectPONumber.Length - 1
If u <> selectPONumber.Length Then
qry = qry + "and B.PONumber = '" & selectPONumber(u) & "' or "
Else
qry = qry + "and B.PONumber = '" & selectPONumber(u) & "'"
End If
Next
qry = qry + ") "
Else
qry = qry + "and B.PONumber = '" & selectPONumber(0) & "' "
End If
End If
Many thanks in advance!
Update
Replaced all lengths with counts, and took tranceporter's advice. Now, it gives the same error on the "for" line. How did it make it past the If selectPONumber.Length > 1 ?
Updated Code
If selectPONumber IsNot Nothing AndAlso selectPONumber.Count > 1 Then
qry = qry + "and ("
For u As Integer = 0 To (selectPONumber.Count - 1)
If u <> selectPONumber.Count Then
qry = qry + "B.PONumber = '" & selectPONumber(u) & "' or "
Else
qry = qry + "B.PONumber = '" & selectPONumber(u) & "'"
End If
Next
qry = qry + ") "
ElseIf selectPONumber(0) <> "" Then
qry = qry + "and B.PONumber = '" & selectPONumber(0) & "' "
End If
Solution
tranceporter's solution is correct. I stupidly treated a regular string as an array elsewhere in my code. It started pointing all over the rest of the function as indexoutofbounds.
try using this (unless you already have)
If selectPONumber IsNot Nothing Then
If selectPONumber.Count > 0 Then
qry = qry + "and ("
For u As Integer = 0 To selectPONumber.Count - 1
If u <> selectPONumber.Count Then
qry = qry + "B.PONumber = '" & selectPONumber(u) & "' or "
Else
qry = qry + "B.PONumber = '" & selectPONumber(u) & "'"
End If
Next
qry = qry + ") "
Else
qry = qry + "and B.PONumber = '" & selectPONumber(0) & "' "
End If
End If
i have a drop down box , it has value that is retrieve from datasqlsource, however i want to get the count value on page load but it seem that it did not count
'Dim adapter As New SqlDataAdapter
'Dim ds As New DataSet
'Dim connectionString = ConfigurationManager.ConnectionStrings("myProject").ConnectionString
'Dim myConn As New SqlConnection(connectionString)
'Dim cmd = "SELECT * FROM Group Where groupID='" & DropDownList1.SelectedValue & "' AND customerID='" & Session("customerID") & "'"
'Try
' myConn5.Open()
' Dim myCmd5 As New SqlCommand(cmd5, myConn5)
' adapter.SelectCommand = myCmd5
' adapter.Fill(ds, "Group")
' ' txtQuan.Text = adapter.SelectCommand.ExecuteScalar().ToString()
' adapter.Dispose()
' myCmd5.Dispose()
' ' txtQuan.Text = ds.Tables(0).Rows.Count
' If ds.Tables(0).Rows.Count >= 10 Then
' lblNoPpl.Text = "The group is full"
' Else
' Dim numLefts As Integer = 10 - ds.Tables(0).Rows.Count
' lblNoPpl.Text = numLefts.ToString() + "space left"
'Catch ex As Exception
' MsgBox("Can not open connection ! ")
'End Try
if you use cmd.ExcuteScalar() method retrieve a count,the sql should be
SELECT COUNT(*) FROM Group Where groupID='" & DropDownList1.SelectedValue & "' AND customerID='" & Session("customerID") & "'"