小编pra*_*rab的帖子

'System.Configuration.ConfigurationSettings.AppSettings'已过时

我收到了以下警告

'System.Configuration.ConfigurationSettings.AppSettings'已过时:''此方法已过时,已被System.Configuration取代!System.Configuration.ConfigurationManager.AppSettings"'

你是如何解决的?

c# class-library class visual-studio

53
推荐指数
4
解决办法
9万
查看次数

错误:索引超出了数组的范围.

我知道这个问题是什么,但我对我的程序如何输出一个超出数组的值感到困惑.

我有一个0到8的整数,这意味着它可以保持9个整数,对吗?我有一个int,检查以确保用户输入值是1-9.我从整数中删除一个(像这样)

if (posStatus[intUsersInput-1] == 0) //if pos is empty
{
    posStatus[intUsersInput-1] += 1; 
}//set it to 1
Run Code Online (Sandbox Code Playgroud)

然后我自己输入9并得到错误.它应该访问数组中的最后一个int,所以我不明白为什么我会收到错误.相关代码:

public int[] posStatus;       

public UsersInput()    
{    
    this.posStatus = new int[8];    
}

int intUsersInput = 0; //this gets try parsed + validated that it's 1-9    

if (posStatus[intUsersInput-1] == 0) //if i input 9 it should go to 8?    
{    
    posStatus[intUsersInput-1] += 1; //set it to 1    
} 
Run Code Online (Sandbox Code Playgroud)

错误:

"Index was outside the bounds of the array." "Index was outside the bounds of …
Run Code Online (Sandbox Code Playgroud)

c# arrays indexing bounds

20
推荐指数
2
解决办法
27万
查看次数

在文本框条目上显示工具提示

我有一个textbox需要以某种方式输入数据.我已经实现了一些单元格验证技术来检查输入后的数据,但我想在输入数据之前向用户提供一些信息.

为此,我想将添加tooltiptextbox当用户进入工具箱弹出,然后当他们开始键入退出.

例如,我有以下代码:

private void YearEdit_Enter(object sender, EventArgs e)
  {
        ToolTip tt = new ToolTip();
        tt.IsBalloon = true;
        tt.InitialDelay = 0;
        tt.ShowAlways = true;
        tt.SetToolTip(YearEdit, "Enter 4 digit year.");
    }
Run Code Online (Sandbox Code Playgroud)

这在用户输入时执行textbox,但是tooltip只有当鼠标悬停在用户上时才会出现textbox.有没有人有任何想法解决这个问题?我认为也许tt.ShowAlways = true可行,但显然不行.

.net c# textbox tooltip

19
推荐指数
1
解决办法
7万
查看次数

选择查询以从SQL Server获取数据

我试图在我的C#代码中运行SQL Select查询.但我总是得到-1输出

int result = command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用deleteinsert工作相同的表...

ConnectString 也没关系.

请检查以下代码

SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=");
conn.Open();

SqlCommand command = new SqlCommand("Select id from [table1] where name=@zip", conn);

//command.Parameters.AddWithValue("@zip","india");
int result = command.ExecuteNonQuery();

// result gives the -1 output.. but on insert its 1
using (SqlDataReader reader = command.ExecuteReader())
{
    // iterate your results here
    Console.WriteLine(String.Format("{0}",reader["id"]));
}

conn.Close();
Run Code Online (Sandbox Code Playgroud)

查询在SQL Server上运行正常,但我不知道为什么只有select查询不起作用.

所有其他查询都有效.

c# sql sql-server asp.net

14
推荐指数
3
解决办法
20万
查看次数

泛型运行时或编译时多态吗?

我读到以下格式属于参数多态,但是我们可以将它分为一个,运行时或编译时多态吗?

public class Stack<T>
{  // items are of type T, which is known when we create the object
   T[] items;  
   int count;  
   public void Push(T item)    {...}
   //type of method pop will be decided when we create the object  
   public T Pop() 
   {...} 
 }  
Run Code Online (Sandbox Code Playgroud)

c# oop generics polymorphism

9
推荐指数
1
解决办法
6093
查看次数

只返回字段时使用AsNoTracking()会有所不同吗?

所以我在EF中执行查询时已经阅读了很多关于使用AsNoTracking()的内容,特别是如果它返回实体,以便在不更新的情况下不保留对事物的引用.

但我也读到AsNoTracking也可以加速查询本身,因为EF不必将查询的每个项目映射到地图中的实体.

问题是,如果我的Linq查询只返回列/行中的值而不是实体类型,那么使用AsNoTracking()是否会加快查询速度?如果不是很明显我不应该使用它,因为它只会使代码混乱?

示例1(我希望使用AsNoTracking():

var result = (from p in context.Pogs
              select p).AsNoTracking();
Run Code Online (Sandbox Code Playgroud)

例2(我的问题......我认为在这里使用没有意义,但我不知道答案):

var result = (from p in context.Pogs
              select p.Name);  // assuming p.Name is a string or something
Run Code Online (Sandbox Code Playgroud)

var result = (from p in context.Pogs.AsNoTracking()
              select p.Name);
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework

9
推荐指数
1
解决办法
1540
查看次数

DataGridView将文本放在RowHeader的ColumnHeader中

是否可以将文本添加到RowHeader的ColumnHeader中.我把自动增量放入我的DataGridView的Rowheaders中,我想把"不" 在那上面.

就像是:

//Autoincrement RowHeaders
foreach (DataGridViewRow row in myDGV.Rows)
{
    row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
}

No. | myColumn 1 | myColumn 2

1   | myText     | myText
2   | myText     | myText

//the "No." column are actually RowHeaders
Run Code Online (Sandbox Code Playgroud)

c# datagridview columnheader rowheader

8
推荐指数
1
解决办法
4090
查看次数

TryParse失败,带负数

我在让TryParse正常工作时遇到了问题.我有一个我几乎确定有效的值列表(因为它们来自我们系统中的另一个组件)但我想确保有适当的错误处理.

以下是我的值的示例列表:

20.00
20.00
-150.00

这是我最初写的方法:

 private decimal CalculateValue(IEnumerable<XElement> summaryValues)
        {
            decimal totalValue = 0;

            foreach (XElement xElement in summaryValues)
            {
                decimal successful;
                Decimal.TryParse(xElement.Value, out successful);
                if (successful > 0)
                    totalValue += Decimal.Parse(xElement.Value);
            }
            return totalValue;
        }
Run Code Online (Sandbox Code Playgroud)

变量'success'在-150.00返回false,所以我添加了NumberStyles:

private decimal CalculateValue(IEnumerable<XElement> summaryValues)
        {
            decimal totalValue = 0;

            foreach (XElement xElement in summaryValues)
            {
                decimal successful;
                Decimal.TryParse(xElement.Value, NumberStyles.AllowLeadingSign, null, out successful);
                if (successful > 0)
                    totalValue += Decimal.Parse(xElement.Value, NumberStyles.AllowLeadingSign);
            }
            return totalValue;
        }
Run Code Online (Sandbox Code Playgroud)

但是,现在我在那里有NumberStyles,没有数字会解析!将IFormatProvider设置为null我感觉很好,因为这都在我们的系统中.有谁看到我可能做错了什么?

c# tryparse

7
推荐指数
2
解决办法
2万
查看次数

使用实体框架在运行时动态选择列

我有这样的现有功能

public int sFunc(string sCol , int iId)
{
    string sSqlQuery = "  select  " + sCol + " from TableName where ID = " +  iId ;
    // Executes query and returns value in column sCol
}
Run Code Online (Sandbox Code Playgroud)

该表有四列存储整数值,我使用上面的函数分别读取它们.

现在我将其转换为Entity Framework.

public int sFunc(string sCol , int iId)
{
     return Convert.ToInt32(TableRepository.Entities.Where(x => x.ID == iId).Select(x => sCol ).FirstOrDefault());
}
Run Code Online (Sandbox Code Playgroud)

但上面的函数返回错误

输入字符串格式不正确

因为它返回列名本身.

我不知道如何解决这个问题,因为我对EF很新.

任何帮助,将不胜感激

谢谢

c# sql asp.net entity-framework formatexception

7
推荐指数
2
解决办法
2万
查看次数

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法

我试图将一个应用程序转换为EntityFrameWork codefirst.我现在的代码是

 string sFrom  ="26/12/2013";
 select * FROM Trans where  CONVERT(datetime, Date, 105) >=   CONVERT(datetime,'" + sFrom + "',105) 
Run Code Online (Sandbox Code Playgroud)

我试过了

 DateTime dtFrom = Convert.ToDateTime(sFrom );
TransRepository.Entities.Where(x  =>Convert.ToDateTime(x.Date) >= dtFrom) 
Run Code Online (Sandbox Code Playgroud)

但是我得到了这样的错误

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法

请提前帮助,谢谢

c# linq-to-entities entity-framework

6
推荐指数
1
解决办法
3万
查看次数