Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So here's my code...

C#
string connectionString = null;
                string sql = null;
                connectionString = "Server=(localdb)\\v11.0;Integrated Security=true;";
                using (SqlConnection cnn = new SqlConnection(connectionString))
                {
                    sql = "INSERT INTO EmailParser (Sender,Building,Level,Phase,Request) values(@Sender,@Building,@Level,@Phase,@Request)";
                    cnn.Open();
                    using (SqlCommand cmd = new SqlCommand(sql, cnn))
                    {
                        String d1 = m.Groups[4].ToString();
                        String d2 = m.Groups[9].ToString();
                        String int1 = m.Groups[14].ToString();
                        String word1 = m.Groups[19].ToString();

                        cmd.Parameters.AddWithValue("@Sender", email.From);
                        cmd.Parameters.AddWithValue("@Building", d1);
                        cmd.Parameters.AddWithValue("@Level", d2);
                        cmd.Parameters.AddWithValue("@Phase", int1);
                        cmd.Parameters.AddWithValue("@Request", word1);
                       
                        cmd.ExecuteNonQuery();}


The d1, d2, int1, and word1 are from the RegEx...

C#
string re4 = "(\\d)";
string re9 = "(\\d)";
string re14 = "(\\d+)";
string re19 = "([A-Za-z0-9/-]+)";


What I have tried:

I think the error is from the parameters but I can't still figure out what's wrong with my code. d1, d2, int1, and word1 are all in string so why can't those be inserted on my database? The data types of the fields are all nvarchar. I assume nvarchar are strings right?
Posted
Updated 10-Dec-17 5:01am
Comments
Richard Deeming 30-Mar-16 10:42am    
What type does the email.From property return?
dumbasian 30-Mar-16 10:45am    
It's a string. What it does is it extracts the email body. I got it from Limilabs.
dumbasian 30-Mar-16 10:46am    
I stand corrected. I am referring to From or Sender of the email.
dumbasian 30-Mar-16 10:50am    
Anyways, thanks for pointing it out! I just noticed that I haven't converted the email.From into string! Thanks! I think I know who's the culprit.

The error comes from sending an object as a parameter value. It needs to be a string, int, bool, etc.

It's probably email.From. Call .ToString() or somehow pass as string.
 
Share this answer
 
cmd = New SqlCommand("insert into EquipmentType values(@EquipmentTypeID,@EquipmentName)", conn)
cmd.Parameters.AddWithValue("@EquipmentTypeID", txtEquipmentTypeID)
cmd.Parameters.AddWithValue("@EquipmentName", txtEquipmentTypeName)
cmd.ExecuteNonQuery()
showEquipmenttype()
txtEquipmentTypeID.Clear()
txtEquipmentTypeName.Clear()
txtEquipmentTypeID.Focus()
 
Share this answer
 
Comments
CHill60 11-Dec-17 4:56am    
This has nothing to do with the question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900