没有数据时读取数据的尝试无效

时间:2014-12-04 11:55:59

标签: c# asp.net

Values Not Being Stored to Database      

cmd = new SqlCommand("ImportExcel", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@UserCategory", SqlDbType.NVarChar, 100).Value = imp.UserCategory;
                cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 200).Value = imp.CompanyName;
                cmd.Parameters.Add("@PrimaryContact", SqlDbType.NVarChar, 200).Value = imp.PrimaryContact;
                cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50).Value = imp.Title;
                cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 100).Value = imp.FirstName;
                cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 100).Value = imp.LastName;
                cmd.Parameters.Add("@LeadIndustryType", SqlDbType.NVarChar, 100).Value = imp.IndustruType;
                cmd.Parameters.Add("@SupplierService", SqlDbType.NVarChar).Value = imp.ServiceCategory;
                cmd.Parameters.Add("@Designation", SqlDbType.NVarChar, 100).Value = imp.Designation;
              //  cmd.Parameters.Add("@AnnualRevenue", SqlDbType.NVarChar, 50).Value = ContactsObj.AnnualRevenue;
                cmd.Parameters.Add("@PhoneCountryCode", SqlDbType.NVarChar, 50).Value = imp.PhoneCountryCode;
                cmd.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar, 50).Value = imp.PhoneNumber;
                cmd.Parameters.Add("@MobileCountryCode", SqlDbType.NVarChar, 50).Value = imp.MobileCountryCode;
                cmd.Parameters.Add("@MobileNumber", SqlDbType.NVarChar, 50).Value = imp.MobileNumber;
                cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 200).Value = imp.Email;
                if (!String.IsNullOrEmpty(imp.EmailOption))
                {
                    cmd.Parameters.Add("@EmailOption", SqlDbType.Bit).Value = imp.EmailOption;
                }
                else
                {
                    cmd.Parameters.Add("@EmailOption", SqlDbType.Bit).Value = DBNull.Value;
                }
                if (!String.IsNullOrEmpty(imp.OfferAlertReceive))
                {
                    cmd.Parameters.Add("@OfferAlertReceive", SqlDbType.Bit).Value = imp.OfferAlertReceive;
                }
                else
                {
                    cmd.Parameters.Add("@OfferAlertReceive", SqlDbType.Bit).Value = DBNull.Value;
                }
                //cmd.Parameters.Add("@SecondaryEmail", SqlDbType.NVarChar, 200).Value = imp.SecondaryEmail;
                cmd.Parameters.Add("@SkypeID", SqlDbType.NVarChar, 200).Value =imp.SkypeID;
                cmd.Parameters.Add("@Fax", SqlDbType.NVarChar, 50).Value = imp.Fax;
                cmd.Parameters.Add("@Website", SqlDbType.NVarChar, 200).Value = imp.Website;
                cmd.Parameters.Add("@PresentAddressLine1", SqlDbType.NVarChar, 200).Value =imp.PresentAddressLine1;
                cmd.Parameters.Add("@PresentAddressLine2", SqlDbType.NVarChar, 200).Value =imp.PresentAddressLine2;
                cmd.Parameters.Add("@PresentCity", SqlDbType.NVarChar, 200).Value =imp.PresentCity;
                cmd.Parameters.Add("@PresentState", SqlDbType.NVarChar, 100).Value = imp.PresentState;
                cmd.Parameters.Add("@PresentCountry", SqlDbType.NVarChar, 100).Value = imp.PresentCountry;
                cmd.Parameters.Add("@PresentZipCode", SqlDbType.NVarChar, 50).Value = imp.PresentZipCode;
                cmd.Parameters.Add("@PermanentAddressLine1", SqlDbType.NVarChar, 200).Value = imp.PermanentAddressLine1;
                cmd.Parameters.Add("@PermanentAddressLine2", SqlDbType.NVarChar, 200).Value = imp.PermanentAddressLine2;
                cmd.Parameters.Add("@PermanentCity", SqlDbType.NVarChar, 200).Value = imp.PermanentCity;
                cmd.Parameters.Add("@PermanentState", SqlDbType.NVarChar, 100).Value = imp.PermanentState;
                if (!String.IsNullOrEmpty(imp.PermanentCountry))
                {
                    cmd.Parameters.Add("@PermanentCountry", SqlDbType.NVarChar, 100).Value = imp.PermanentCountry;
                }
                else
                {
                    cmd.Parameters.Add("@PermanentCountry", SqlDbType.NVarChar, 100).Value = DBNull.Value;
                }
                cmd.Parameters.Add("@PermanentZipCode", SqlDbType.NVarChar, 50).Value = imp.PermanentZipCode;
                if (!String.IsNullOrEmpty(imp.GroupName))
                    {
                        cmd.Parameters.Add("@OfferAlertType", SqlDbType.NVarChar, 100).Value = imp.OfferAlertType;
                    }
                    else
                    {
                        cmd.Parameters.Add("@OfferAlertType", SqlDbType.NVarChar, 100).Value = DBNull.Value;
                    }
                if (!String.IsNullOrEmpty(imp.GroupName))
                {
                    cmd.Parameters.Add("@GroupName", SqlDbType.NVarChar, 100).Value = imp.GroupName;
                }
                else
                {
                    cmd.Parameters.Add("@GroupName", SqlDbType.NVarChar, 100).Value = DBNull.Value;
                }
                cmd.Parameters.Add("@LeadSource",SqlDbType.NVarChar,100).Value=imp.LeadSource;
                    cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 300).Value = imp.Description;
                    cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar, 100).Value = imp.CreatedBy;
                    cmd.Parameters.Add("@ip", SqlDbType.NVarChar, 50).Value = imp.ip;
                    con.Open();
                    SqlDataReader dr1 = cmd.ExecuteReader();               
                    //dr1 = cmd.ExecuteReader();
                    string st = string.Empty;
                    while (dr1.Read())
                    {
                         st = dr1[0].ToString();
                    }

              st = dr1[0].ToString();
                return st;

1 个答案:

答案 0 :(得分:0)

while循环后删除此行: -

st = dr1[0].ToString();

您需要调用Read()方法实际读取DataReader对象的日期。

此外,如果您将代码包装在using块中会更好: -

using (SqlDataReader dr1 = cmd.ExecuteReader())
{
   while (dr1.Read())
   {
       st = dr1[0].ToString(); //With Index
       st = dr1["ColumnName"].ToString(); //With Column Name
   }
}

<强>更新 根据您的评论,这是一个插入SP,您试图读取返回值,因此您不能使用SqlDataReader,如果您想要返回SP是否成功插入数据,您可以返回类似{{的状态代码1}} 1&amp; successfull insertion因失败或其他原因。然后,您可以像这样阅读此返回代码: -

0

但是如果你想返回一个像你提到SqlParameter returnParameter = cmd.Parameters.Add("StatusCd", SqlDbType.Int); returnParameter.Direction = ParameterDirection.ReturnValue; cmd.ExecuteNonQuery(); int statusCode = (int)returnParameter.Value; 的文本,你需要将其添加为Successfully Inserted,因为SQL只能返回整数值,然后你可以将这个输出值检索为: -

Output parameter

此处,@ code将是您SP中的输出参数,您可以将其设置为:SqlParameter output = new SqlParameter("@code", SqlDbType.Int); output.Direction = ParameterDirection.Output; cmd.Parameters.Add(output); cmd.ExecuteNonQuery(); st = output.Value.ToString();