无法在SQL2005&中编写查询c#asp.net [英] Unable to write query in SQL2005 & c# asp.net

查看:52
本文介绍了无法在SQL2005&中编写查询c#asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试下面的代码来显示来自Call_reg表的Complaint_no以及来自数据库的new_tech表中的tech_name以及选择投诉号后。从下拉列表中,我想从另一个下拉列表中选择技术人员名称然后点击按钮点击插入Call_Allocation表



点击按钮我无法编写插入查询同样的。



错误代码以粗体显示。



有人可以帮我吗....





Hi,

I am trying below code to display Complaint_no from Call_reg table as well as tech_name from new_tech table from database and after selecting complaint no. from dropdownlist, i want to choose technician name from another dropdownlist and then on button click to be inserted in Call_Allocation table

On button click i am unable to write query for inserting the same.

Error code mentioned as bold.

Can somebody help me please....


SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    private string s;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlComplaint.Items.Insert(0, new ListItem("---Select---", "---Select---"));
            FillDropDownList();
            ddlAllow.Items.Insert(0, new ListItem("---Select---", "---Select---"));
            FillTechnicianDropDownList();
        }
    }
   
   

    // Fill Dropdownlist
    public void FillDropDownList()
    {
        s = WebConfigurationManager.ConnectionStrings["Callregister_connectionstring"].ConnectionString;
        con = new SqlConnection(s);
        con.Open();
        cmd = new SqlCommand("Select Complaint_no from Call_Reg where Status='Open'" , con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            ddlComplaint.Items.Add(dr[0].ToString());
        }
        dr.Close();
        con.Close();
    }

    // Show data in GridView
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        s = WebConfigurationManager.ConnectionStrings["Callregister_connectionstring"].ConnectionString;
        con = new SqlConnection(s);
        con.Open();
        cmd = new SqlCommand("Select * from Call_Reg where Complaint_no='" + ddlComplaint.SelectedItem.ToString() + "'", con);
        dr = cmd.ExecuteReader();
        GridView2.DataSource = dr;
        GridView2.DataBind();
        dr.Close();
        con.Close();
    }

    // Fill Technician 
    public void FillTechnicianDropDownList()
    {
        s = WebConfigurationManager.ConnectionStrings["Callregister_connectionstring"].ConnectionString;
        con = new SqlConnection(s);
        con.Open();
        cmd = new SqlCommand("Select Fname from New_Tech", con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            ddlAllow.Items.Add(dr[0].ToString());
        }
        dr.Close();
        con.Close();
    }

    protected void btnAllo_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("SERVER=KSHITIJA-PC; Initial Catalog=CRM;Integrated Security=True");
        //SqlCommand cmd = new SqlCommand("Update Call_reg set status='Assigned' Where Complaint_no= '" + ddlComplaint.SelectedItem.ToString() + "'", con);
        SqlCommand cmd = new SqlCommand(" insert into Call_Allocation values('select a.Fname from New_Tech a join Call_reg b on a.Fname=b.Complaint_no '", con);
        try
        {
            con.Open();

            int i = cmd.ExecuteNonQuery();
            //int a = cmd1.ExecuteNonQuery();
            if (i  == 1)
            {
                ddlComplaint.SelectedItem.Text = "";
                EO.Web.MsgBoxButton mb = new EO.Web.MsgBoxButton("OK");
                MsgBox1.Show("Message: ", "Call Assigned to Technician", null, mb);
             }

        }
        catch (Exception ex)
        {
            EO.Web.MsgBoxButton mb = new EO.Web.MsgBoxButton("OK");
            MsgBox1.Show("Error: ", ex.Message, null, mb);

        }
        con.Close();
    }

推荐答案

你将select语句放在引号中,所以你的sql将SQL作为字符串传递给要插入的值。那肯定不是你想要的。
You''re putting your select statement in quotes, so your sql is passing SQL as a string as a value to insert. That is not what you want, surely.


这篇关于无法在SQL2005&中编写查询c#asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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