小编Joh*_*ers的帖子

从'System.String'到'System.Guid'的转换无效

我有一个提交表单,由DropDownList控件组成; 它的ListItem值由UniqueIdentifier组成,例如524988aa-0594-4ebe-b7fa-61396855e17f.

我试图通过存储过程将ListItem的值插入到数据库中的表中,但在提交时会出现"从'System.String'到'System.Guid'的无效转换"错误.

超文本:

 <asp:DropDownList ID="ddlserver" runat="server">
    <asp:ListItem Text="Server 1" Value="88dc9e7d-0904-4a49-a6d9-8d0badfc4d0b" />
    <asp:ListItem Text="Server 2" Value="524988aa-0594-4ebe-b7fa-61396855e17f" />
 </asp:DropDownList> 
Run Code Online (Sandbox Code Playgroud)

代码背后:

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertClient";
cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = ddlserver.SelectedValue.ToString();
cmd.Parameters.Add("@ClientName", SqlDbType.NVarChar).Value = txtName.Text;
cmd.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar).Value = txtPhone.Text;
cmd.Parameters.Add("@ServicesNum", SqlDbType.NVarChar).Value = rbzero.Text;
cmd.Connection = con;
Run Code Online (Sandbox Code Playgroud)

存储过程:

ALTER PROCEDURE [dbo].[InsertClient]

    @UserId UniqueIdentifier,
    @ClientName nvarchar(150),
    @EmailAddress nvarchar(150),
    @PhoneNumber nvarchar(50),
    @ServicesNum nvarchar(50)

AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO  Client_tb (UserId, ClientName, EmailAddress, PhoneNumber, ServicesNum) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net

0
推荐指数
1
解决办法
1658
查看次数

条件数的计数值

我试图计算表AUTHORIZED_USERS中存在登录用户ID的实例数.我正在寻找的值可以在UNAME字段中找到,它是一个包含以下格式的字符串:COMPANYNAME\111222333.如果表中存在该值,则表示用户有权访问应用程序/视图.否则,它们将被重定向到拒绝访问的页面.我的代码接近工作,但出现以下错误

Incorrect syntax near '\601011307'.
Run Code Online (Sandbox Code Playgroud)

\ 601011307是表UNAME字段中包含的条目的一部分.它应该识别的全部值是COMPANYNAME\601011307.

如何防止此错误并将计数值分配给变量,以便我可以在条件中使用它?

 public ActionResult HolidayDateTable()
    {
        string whoareyoupeople = User.Identity.Name.ToString();
        DateTime date = DateTime.Now;
        string myerrorstring = "User " + whoareyoupeople + " attempted unauthorized access on " + date + ".";
        SqlConnection conn = new SqlConnection("Data Source=SWDB10DSQL;Initial Catalog=BillingUI;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        {
            cmd.CommandText = string.Format("SELECT COUNT(*) FROM AUTHORIZED_USERS WHERE UNAME = "+whoareyoupeople+")");

            int count = (int)cmd.ExecuteScalar();

            if (count == 0)
            {
                return RedirectToAction("AccessDenied");
            }
            else
            {
                return View(db.HOLIDAY_DATE_TABLE);
            } …
Run Code Online (Sandbox Code Playgroud)

c# sql sql-server visual-studio-2012

0
推荐指数
1
解决办法
55
查看次数

无法将类型'int'隐式转换为'string []'?

我必须计算桌子上的一排.根据这个数字,我必须分配团队.使用下面给出的代码显示错误无法隐式将类型'int'转换为'string []'.

码:

protected void ddlCircle_SelectedIndexChanged(object sender, EventArgs e)
    {
      ArrayList teamList = new ArrayList();

      ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
      cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
      DataTable dt = new DataTable();
      dt = cd.GetAvailableData(ddlCircle.SelectedValue);

      int x;

      x = dt.Rows.Count;

      String[] a;


      for (int i = 1; i <= x; i++)
        {
           a = teamList.Add(String.Format("Team{0}", i)); // error popup here
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# asp.net

0
推荐指数
1
解决办法
661
查看次数

如何在params对象[]前面插入一个对象

public class bar:foo
{
    public bar(int something, params object[] parameters)
        :base(something, parameters) //foo will receive {int, object[]}, not what i want
    {
    }
}

public class foo
{
    public foo(params object[] parameters)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

所以基本上我想在传递给基类的 params 数组前面再附加一个对象。假设我称bar(1, 2, 3, 4, 5)我希望 fooreceive {1, 2, 3, 4, 5}作为参数而不是 {1, {2, 3, 4, 5}} ,这是上面的代码给我的。

c#

0
推荐指数
1
解决办法
1920
查看次数

错误:正在初始化参数1

我环顾四周,看到了其中的一些,但没有一个为我的问题提供解决方案.我使用以下代码获得此编译错误:

错误:

在此输入图像描述

代码:

const int TOP_WORDS = 25;

...

void topWords(Hash t, string word, string topA[]); 

int main()
{
    ...
    Hash table1;
    string word = "example";

    string topWordsArr[TOP_WORDS];

    table1.addItem(word);
    topWords(table1, word, topWordsArr);

    ...
}

...

void topWords(Hash t, string word, string topA[])
{
    int i = 0;
    int tempCount = t.itemCount(word);
    int tempCount2 = t.itemCount(topA[i]);

    while (tempCount > tempCount2 && i < TOP_WORDS) {
        i++;
        tempCount2 = t.itemCount(topA[i]);
    }

    if (i > 0)
Run Code Online (Sandbox Code Playgroud)

我看到的关于这个错误的所有其他帖子都涉及声明/传递字符串数组参数的错误语法,但我已经对它进行了双重和三重检查,我确信它是正确的; 虽然我以前错了..

c++ arrays function function-parameter

0
推荐指数
1
解决办法
9160
查看次数

如何在powershell函数中使用c#switch参数?

我有一个功能

getinfo([string]$subcommand, [string]$argument)
Run Code Online (Sandbox Code Playgroud)

我想做的是让参数值为'-h'或'-help'

getinfo hosts -h
Run Code Online (Sandbox Code Playgroud)

将为子命令'hosts'提供帮助文本.不幸的是,当我指定-h时,我认为它不会将"-h"的值赋给$参数,而是创建一个新参数.

我正在考虑使用switch参数,但我不确定我是否可以在函数中使用它与上面的$ argument一起使用.

这可能吗?

parameters powershell function

0
推荐指数
1
解决办法
489
查看次数

将HTML转换为word格式

我使用word自动化来创建word文档.我想在该文件中嵌入一些HTML代码.我应该如何将html标签转换为word文档格式?(我想在html中保留字体,粗体,表格和其他样式)

html c# asp.net office-automation

0
推荐指数
1
解决办法
8718
查看次数

在SQL Server 2012 Decimal列中插入double会导致不精确

在从C#到SQL服务器没有任何不精确的情况下,让程序插入实际数字会遇到很多麻烦.

例如,以下是在推送到SQL Server之前如何在C#中处理数据的代码:

DataTable dt = new DataTable();
dt.Columns.Add("Number", typeof(double));

dt.Rows.Add(6.5);
dt.Rows.Add(7);
dt.Rows.Add(8);

SqlConnection con;
string sqlCmd;
SqlCommand cmd;

using (con = new SqlConnection(Common.DBLink))
{
                con.Open();
                SqlBulkCopy bulkCopy = new SqlBulkCopy(con);
                bulkCopy.DestinationTableName = "NumberTable";
                bulkCopy.WriteToServer(dt);
                con.Close();
}
Run Code Online (Sandbox Code Playgroud)

但如果我有一个像2.85这样的数字,它会被插入为2.8499.SQL列是一种DECIMAL(10,4)数据类型.这只发生在很少的数字上.对于像12.5或20这样的值,它永远不会发生.

c# sql-server double decimal sql-server-2012

0
推荐指数
1
解决办法
1005
查看次数

从Object获取数组

我有以下类定义.

public class people
{
    public string first_name { get; set; }
    public string last_name  { get; set; }
    public DateTime date_of_birth  { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了一系列人,如下所示:

    people[] the_people = new people[3];
    the_people[0].first_name="Tony";
    the_people[0].last_name="Carrot";
    the_people[0].date_of_birth=new DateTime(1959-03-16);
    the_people[1].first_name="Joe";
    the_people[1].last_name="Tomato";
    the_people[1].date_of_birth=new DateTime(1963-06-2);
    the_people[2].first_name="Tarina";
    the_people[2].last_name="Wends";
    the_people[2].date_of_birth=new DateTime(1982-11-22);
Run Code Online (Sandbox Code Playgroud)

如何将the_people对象的first_names存储在新的字符串数组中,以便获得如下所示的输出.这可能通过linq

string[] the_peoples_first_names=new string[3] {"Tony","Joe","Tarina"}; 
Run Code Online (Sandbox Code Playgroud)

类似地,我如何获得一个日期时间数组,以便将所有人的出生日期存储在单独的DateTime数组中.

c# arrays class object

0
推荐指数
1
解决办法
67
查看次数

如何对字符串扩展方法进行单元测试

我有一些如下代码,我想对字符串类的扩展方法进行单元测试:

public static string Replace(this string str, Dictionary<string, string> dict)
{            
}
Run Code Online (Sandbox Code Playgroud)

我的扩展方法在主项目中,因此我的单元测试项目甚至看不到它。

我怎样才能做到这一点?我正在使用Nunit。

c# nunit unit-testing

0
推荐指数
1
解决办法
1696
查看次数