没有得到理想的输出 [英] Not getting desired output

查看:62
本文介绍了没有得到理想的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,我有用于在asp.net中插入数据的代码c#4.5





这里是代码:

--------------------------------

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Web.UI.WebControls;

public partial class 注册:System.Web.UI.Page
{
SqlConnection con;
SqlCommand命令;
SqlDataAdapter sda;
受保护 void Page_Load( object sender,EventArgs e)
{

}
protected void txtsubmit_Click( object sender,EventArgs e)
{
string register_id = txtReg_id.Text;
string name = txtName.Text;
string contact = txtcontact.Text;

con = new SqlConnection( Data Source = TUSHAR \\ SQL; Initial Catalog = clrroom; Integrated Security = True);
con.Open();
尝试
{
string insertq = 插入寄存器(Registration_id,名称,联系人,角色) + 值(@ Registration_id,@ Name,@ Contact,@ Role);
command = new SqlCommand(insertq,con);
command.Parameters.Add( @ Registration_id,SqlDbType.VarChar).Value = txtReg_id.Text.Trim();
command.Parameters.Add( @ Name,SqlDbType.VarChar).Value = txtName.Text.Trim();
command.Parameters.Add( @ contact,SqlDbType.VarChar).Value = txtcontact.Text.Trim();
command.Parameters.Add( @ Role,SqlDbType.VarChar).Value = drdpRole.SelectedItem;
command.ExecuteNonQuery();

}
catch (exception ex_)
{
Response.Write( 发生错误 + ex_.Message + );
}
最后
{
con.Close();
}

}
}





我尝试过:



先生

i在首次执行此页面时忽略了它,但显示此消息=错误发生失败转换参数值从ListItem到String。

解决方案

 command.Parameters.Add(   @ Role,SqlDbType.VarChar).Value = drdpRole.SelectedItem; 



< a href =https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selecteditem(v=vs.110).aspx> ListControl.SelectedItem属性(System.Web .UI.WebControls) [ ^ ]返回 ListItem ,而不是 String 。您应该首先查看文档,以获取此类消息。


您好,

更改

 command.Parameters.Add(< span class =code-string>  @ Role,SqlDbType.VarChar).Value = drdpRole.SelectedItem; 



to

 command.Parameters.Add(  @ Role,SqlDbType.VarChar).Value = drdpRole.SelectedItem.Text; 





感谢


dear sir i haave the code for inserting data into data in asp.net c# 4.5


here 's the code:
--------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Register : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand command;
    SqlDataAdapter sda;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void txtsubmit_Click(object sender, EventArgs e)
    {
        string register_id = txtReg_id.Text;
        string name = txtName.Text;
        string contact = txtcontact.Text;

        con = new SqlConnection("Data Source=TUSHAR\\SQL;Initial Catalog=clrroom;Integrated Security=True");
        con.Open();
        try
        {
            string insertq = "insert into Register(Registration_id, Name,Contact,Role)"+ "values(@Registration_id,@Name,@Contact,@Role)";
            command = new SqlCommand(insertq,con);
            command.Parameters.Add("@Registration_id",SqlDbType.VarChar).Value=txtReg_id.Text.Trim();
            command.Parameters.Add("@Name",SqlDbType.VarChar).Value=txtName.Text.Trim();
            command.Parameters.Add("@contact",SqlDbType.VarChar).Value=txtcontact.Text.Trim();
            command.Parameters.Add("@Role",SqlDbType.VarChar).Value= drdpRole.SelectedItem;
            command.ExecuteNonQuery();

        }
        catch(Exception ex_)
        {
            Response.Write("Error occured"+ ex_.Message + "");
        }
        finally
        {
            con.Close();
        }

    }
}



What I have tried:

sir
i have ignored it after first execution of this page but shows this message ="Error occuredFailed to convert parameter value from a ListItem to a String."

解决方案

command.Parameters.Add("@Role",SqlDbType.VarChar).Value= drdpRole.SelectedItem;


The ListControl.SelectedItem Property (System.Web.UI.WebControls)[^] returns a ListItem, not a String. You should always check the documentation first for messages such as this.


Hello ,
Change

command.Parameters.Add("@Role",SqlDbType.VarChar).Value= drdpRole.SelectedItem;


to

command.Parameters.Add("@Role",SqlDbType.VarChar).Value= drdpRole.SelectedItem.Text;



Thanks


这篇关于没有得到理想的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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