更改ddl的索引时访问数据frm表 [英] access data frm table when index change of ddl

查看:61
本文介绍了更改ddl的索引时访问数据frm表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下拉列表&的选定索引更改时访问数据库表中的数据.在现有标签上显示

我编写了如下代码,其中ddlweek7是Dropdownlist&从lbl开始的首字母是标签

 受保护的 无效 DDLWEEK7_SelectedIndexChanged(对象发​​件人,EventArgs e)
    {

OleDbConnection con =  OleDbConnection(" );
    
 OleDbCommand cmd3 = 新建 OleDbCommand("  + DDLWEEK7.SelectedItem,con);
    
 con.Open();
     
OleDbParameter op =  OleDbParameter();
     op.ParameterName = " ;
     op.Value = DDLWEEK7.SelectedItem.ToString();
     cmd3.Parameters.Add(op);

OleDbDataReader DR1 = cmd3.ExecuteReader();

       
lblfrmdt.Text + = DR1.ToString();

} 


为此,我收到了错误消息,因为[在处理命令期间发生一个或多个错误.ORA-00936:缺少表达式]

解决方案

不确定为什么要连接一个值您在查询上指定了参数的时候.尝试将OleDbCommand的代码替换为此.

 OleDbCommand cmd3 =  OleDbCommand(" ,同)); 


同样,此(op.Value = DDLWEEK7.SelectedItem.ToString();)将仅返回所选项目的类型,即ListItem,而不返回值.我不确定您使用此工具的目的.尝试改用DDLWEEK7.SelectedValue.或者,如果要选择项目的索引,请使用DDLWEEK7.SelectedIndex属性.请在此处 [



在您后面的代码中:

string OraConn = ConfigurationManager.ConnectionStrings["ORAConnection"].ConnectionString;  
   //string retVal = string.empty;   
   OleDbConnection oconn;
   OleDbCommand ocmd;
   OleDbDataReader odr;
   try
   {
       using (oconn = new OleDbConnection(OraConn))
       {
           oconn.Open();
           string query = "SELECT d1 FROM VIPUL_DATE WHERE dw =''" + UserId + "''";
           using (ocmd = new OleDbCommand(query, oconn))
           {
               OleDbDataReader odr = ocmd.ExecuteReader();
               if (odr.Read())
               {
                  retVal = odr["d1"].ToString();  // U should handle this if null...
               }
           }
      }
   }
   
   catch(Exception ex)
   {
        throw ex;
   }
   lblfrmdt.Text = retVal;



问候,

代数


i want to access data from database table at the selected index change of dropdownlist & show on to the existing label for which

I write a code as follow where ddlweek7 is Dropdownlist & initials starting from lbl are the labels

protected void DDLWEEK7_SelectedIndexChanged(object sender, EventArgs e)
    {

OleDbConnection con = new OleDbConnection("Provider=MSDAORA;User ID=sd;password=s321;Data Source=User");
    
 OleDbCommand cmd3 = new OleDbCommand("SELECT d1  FROM VIPUL_DATE WHERE dw=@p1"+DDLWEEK7.SelectedItem, con);
    
 con.Open();
     
OleDbParameter op = new OleDbParameter();
     op.ParameterName = "@p1";
     op.Value = DDLWEEK7.SelectedItem.ToString();
     cmd3.Parameters.Add(op);

OleDbDataReader DR1= cmd3.ExecuteReader();

       
lblfrmdt.Text+=DR1.ToString();

}


for this i got the error as [One or more errors occurred during processing of command.ORA-00936: missing expression]

Not sure why you are concatenating a value on your query while you have specified a parameter on it. Try replacing the code of your OleDbCommand to this.

OleDbCommand cmd3 = new OleDbCommand("SELECT d1  FROM VIPUL_DATE WHERE dw=@p1", con);


Also, this (op.Value = DDLWEEK7.SelectedItem.ToString();) will only return the type of the selected item, which is ListItem, and not the value. Im not sure about your purpose of using this. Try using DDLWEEK7.SelectedValue instead. Or if you want the index of the selected item, use DDLWEEK7.SelectedIndex property. Refer
here[^] for more details about the DropDownList class.


Hi,

Your code is not clear...
What is you objectives?
Maybe you want to retrieve one record in oracle table.
Right?... If so...
See this example:

In you Web.config:

   <connectionStrings>
  <add name="ORAConnection" connectionString="Provider=MSDAORA;Data Source=User;User ID=sa;Password=s321"/>
</connectionStrings>



In your code behind:

string OraConn = ConfigurationManager.ConnectionStrings["ORAConnection"].ConnectionString;  
   //string retVal = string.empty;   
   OleDbConnection oconn;
   OleDbCommand ocmd;
   OleDbDataReader odr;
   try
   {
       using (oconn = new OleDbConnection(OraConn))
       {
           oconn.Open();
           string query = "SELECT d1 FROM VIPUL_DATE WHERE dw =''" + UserId + "''";
           using (ocmd = new OleDbCommand(query, oconn))
           {
               OleDbDataReader odr = ocmd.ExecuteReader();
               if (odr.Read())
               {
                  retVal = odr["d1"].ToString();  // U should handle this if null...
               }
           }
      }
   }
   
   catch(Exception ex)
   {
        throw ex;
   }
   lblfrmdt.Text = retVal;



Regards,

Algem


这篇关于更改ddl的索引时访问数据frm表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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