In here i need to create a DataTable,But it doesn’t return a id.There’s no error.Actually here i provide a email address(epost).and i need to return id from database.
Coding Part Goes Here.
public string GetAllMails()
{
DataConnection dc = new DataConnection();
string e="";
int uid=-1,sid=0;
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("uid");
dt.Columns.Add("email");
Microsoft.Office.Interop.Outlook.Application oApp;
Microsoft.Office.Interop.Outlook._NameSpace oNS;
Microsoft.Office.Interop.Outlook.MAPIFolder oFolder;
Microsoft.Office.Interop.Outlook._Explorer oExp;
oApp = new Microsoft.Office.Interop.Outlook.Application();
oNS = (Microsoft.Office.Interop.Outlook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
Microsoft.Office.Interop.Outlook.Items items = oFolder.Items;
SqlConnection conn = dc.SqlConnection;
if (conn.State != ConnectionState.Open) conn.Open();
SqlCommand cmd = new SqlCommand("GetDetailsByEmail", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
{
if (mail.UnRead == true)
{
e=mail.SenderEmailAddress;
// param <-- add sql parameters
cmd.Parameters.Add(new SqlParameter("@epost",e));
uid = cmd.ExecuteNonQuery();
//send to db and take the id (uid)
//if returned id
DataRow row = dt.NewRow();
row["id"] = sid;
sid++;
row["uid"] = uid;// uid
row["email"] = e;
dt.Rows.Add(row);
}
}
return e;
}
Stored Procedure goes here
ALTER Procedure [dbo].[GetDetailsByEmail]
@epost nvarchar(50)
AS
BEGIN
Select Medlems_ID,[E-post]
from dbo.Members
where [E-post] = @epost
END
When i edit my coding,uid is a int.
uid = cmd.ExecuteReader(); or uid = cmd.ExecuteScalar(); // it shows me conversion error.here i put as a comment
If you read the documentation for
ExecuteNonQueryyou will see the line:You need to use
SqlCommand.ExecuteReaderto do what you want.EDIT:
Here is how your code could look based on your comment. It is a bit strange that your sproc returns 2 columns but you are only interested in 1. Also it is not clear if you expect multiple rows or not.