商店程序无法访问该值 [英] value not accessible from store procedure

查看:97
本文介绍了商店程序无法访问该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写代码在asp.net MVC中发送电子邮件并且它有效但问题是我在发送邮件时不得不提到FROM字段所以我试图通过编写一段代码来解决这个问题,该代码将empID发送到存储过程并获取一旦会话开始,EMAILADD会针对每个用户,但问题是它没有从SP获取任何记录并返回null为什么? +错误:



指定的字符串不是电子邮件地址所需的格式。



SP :(正常工作)



  ALTER   PROCEDURE  [dbo]。[GetEmailAdd_Sp] 
@ emplid int
AS
BEGIN
声明 @ emplEmail varchar 50

- 添加SET NOCOUNT ON以防止额外结果来自
- 干扰SELECT语句。
< span class =code-keyword> SET NOCOUNT ON ;

选择 emailAdd 来自 HrEmployee 其中 EmplID = @ emplid
- return @emplEmail

END



代码:

  public  ActionResult dailyLog( String  txtEmailTo ,字符串 txtCC,字符串 txtSubject,字符串 txtBody)
{
if (!String.IsNullOrEmpty(Session [ < span class =code-string> Employee] as string ))
{
int emplid = Convert.ToInt32(Session [ Employee]);
String emailFrom = Convert.ToString(DataContext.GetEmailAdd_Sp(emplid).FirstOrDefault());
SmtpClient SmtpServer = new SmtpClient( mail .precisetech.com.pk);
var mail = new MailMessage();
mail.From = new MailAddress(emailFrom);

string [] to = txtEmailTo.Split(' ;');
foreach string mailTo in to)
{
mail.To.Add(mailTo);
}

string [] cc = txtCC.Split('' ;');
foreach string copyTo in cc)
{
MailAddress cCopy = new MailAddress(txtCC);
mail.CC.Add(copyTo);
}
mail.Subject = txtSubject;
mail.IsBodyHtml = true ;
// mail.CC = txtCC;
string htmlBody;
htmlBody = txtBody;
mail.Body = txtBody;
SmtpServer.Port = 25 ;
SmtpServer.UseDefaultCredentials = false ;
SmtpServer.Credentials = new System.Net.NetworkCredential(emailFrom, );
SmtpServer.EnableSsl = false ;



尝试 {
SmtpServer.Send(mail);
ViewBag.Confirmation = 每日日志已被提交;
return View();
}

catch (例外情况)
{
ViewBag.Confirmation = ex.Message;
return View();
}
}





会话员工从previos登录页面存储Empid,即会话[员工] =转换.ToString(EmplID);

解决方案

您需要做的第一件事是使用调试器查看实际情况。

您需要知道 DataContext.GetEmailAdd_Sp(emplid)返回的内容,可能是 emplid 中的实际内容,并确切地将其放入 emailFrom - 因为错误消息是特定的,但无益:指定的字符串不是电子邮件地址所需的格式。并没有告诉你多少,但如果emailFrom中的值为空,因为其余代码返回null - FirstOrDefault应该这样做 - 那么你需要查看源代码:首先要知道的是你在吃什么价值,你得到什么价值。



我们可以为你做这件事 - 我们无权访问您的代码或数据库 - 因此您需要自己查看数据。如果您知道这一点,它应该变得更加清晰,或者至少向我们提供一些工作形式的信息并为您提供新的途径进行调查。


使用.TryParse而不是convert.ToInt

i wrote code to send emails in asp.net MVC and it worked but issue was that i had to mention FROM field during sending mail so i tried to resolve that by writing a piece of code which send empID to store procedure and fetches EMAILADD from there against each user once session begins but problem is that it isn't getting any record from SP and returns null why ? + error:

The specified string is not in the form required for an e-mail address.

SP: (working OK)

ALTER PROCEDURE [dbo].[GetEmailAdd_Sp]
	@emplid int
	AS
BEGIN
	Declare @emplEmail varchar(50)

	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

   Select emailAdd from HrEmployee where EmplID = @emplid
	--return @emplEmail

END


Code:

public ActionResult dailyLog(String txtEmailTo, String txtCC, String txtSubject, String txtBody) 
        {   
            if (!String.IsNullOrEmpty(Session["Employee"] as string))
            {
                int emplid = Convert.ToInt32(Session["Employee"]);
                String emailFrom = Convert.ToString(DataContext.GetEmailAdd_Sp(emplid).FirstOrDefault());
                SmtpClient SmtpServer = new SmtpClient("mail.precisetech.com.pk");
                var mail = new MailMessage();
                mail.From = new MailAddress(emailFrom);

                string[] to = txtEmailTo.Split(';');
                foreach (string mailTo in to)
                {
                    mail.To.Add(mailTo);
                }

                string[] cc = txtCC.Split(';');
                foreach (string copyTo in cc) 
                {
                    MailAddress cCopy = new MailAddress(txtCC);
                    mail.CC.Add(copyTo);
                } 
                mail.Subject = txtSubject;
                mail.IsBodyHtml = true;
               // mail.CC = txtCC;
                string htmlBody;
                htmlBody = txtBody;
                mail.Body = txtBody;
                SmtpServer.Port = 25;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new System.Net.NetworkCredential(emailFrom, "");
                SmtpServer.EnableSsl = false;
               
			
	
	     try {
                SmtpServer.Send(mail);
                ViewBag.Confirmation = "Daily Log Has Been Submitted";
                return View();
             }
         
         catch (Exception ex) 
             {
               ViewBag.Confirmation = ex.Message;
               return View();
  	         }
      }



Session Employee stores Empid from previos login page i.e. Session["Employee"] = Convert.ToString(EmplID);

解决方案

The first thing you need to do is look at what is actually going on, using the debugger.
You need to know what is being returned by DataContext.GetEmailAdd_Sp(emplid), propbably what is actually in emplid, and exactly what gets put into emailFrom - because the error message is specific, but unhelpful: "The specified string is not in the form required for an e-mail address." doesn't tell you much, but if the value in emailFrom is blank because the rest of the code is returning a null - which FirstOrDefault is supposed to do - then you need to look at the sources: and the first thing to find out is what values are you feeding in, and what values are you getting back.

We can;t do that for you - we don't have access to your code, or your database - so you need to look at the data for yourself. When you know that, it should become clearer, or at least give us some information to work form and suggest new avenues for you to investigate.


Used .TryParse instead of convert.ToInt


这篇关于商店程序无法访问该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆