0
dateTimePicker2.Value = Convert.ToDateTime((sdr[1]),   
                        CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);

我正在从 SqlDataReader 中提取日期值,但读取它会导致错误:

指数数组的边界之外

cmd = new SqlCommand("Physio_cure_Search", con);
cmd.CommandType = CommandType.StoredProcedure;   
cmd.Parameters.Add("@RegisterNo", SqlDbType.VarChar).Value = txtsearch.Text;    
SqlDataReader sdr = cmd.ExecuteReader();    
while (sdr.Read())
{
    txtregistrationno.Text = sdr.GetString(0);
    //giving error on this line:    
    dateTimePicker2.Value = Convert.ToDateTime((sdr[1]), System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);        
}

sdrSqlDataReadersdr[1] 的实例,用于检索数据库值。如何解决这个问题?

ALTER procedure [dbo].[Physio_cure_Search]
@RegisterNo varchar(500)   
As
begin
    select Convert(varchar(100),DateOfBirth,103) as DateOfBirth 
    from Physio_cureTable

    select RegisterNo,RegistrationDate,Stimulation,PationName,DateOfBirth,ContactNo,Occupation,Age,
    Sex,Weight,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Ref_By_Doctor,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation 

    from Physio_cureTable where RegisterNo=@RegisterNo and Syncoperation <>'D'
end
4

1 回答 1

0

您有两个问题:您的存储过程返回 2 个结果集,而您正在对第一个结果集进行操作。其次,您的 C# 代码将不起作用。

1-删除第一个结果集select Convert(varchar(100),DateOfBirth,103) as DateOfBirth from Physio_cureTable。这对您的 C# 代码毫无用处。

2-修改您的代码以按列名显式检索值,而不是按数字索引

 while (sdr.Read())
 {
     txtReg.Text = sdr["RegisterNo"].ToString();
     dateTimePicker2.Value = 
         Convert.ToDateTime(sdr["RegistrationDate"].ToString(),   
                            CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
 } 

3- 如果此存储过程仅由此调用代码使用,则从您的选择语句中删除所有未使用的列。

select  RegisterNo, RegistrationDate
from Physio_cureTable where RegisterNo=@RegisterNo and Syncoperation <>'D'
于 2013-03-15T21:12:03.820 回答