Ameen Chaabani
Ameen Chaabani

Reputation: 191

Bind data from label to SQL data source

I want to bind data from label in DataListControl to SQL data source and view it as GridView in other page.

I don't know how to get data from label and put it in database ...

I tried to code this but what I need is the way to GetData from label and put in to the variable.

I have created:

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

public partial class Products : System.Web.UI.Page
{
   // string Name = Label.Equals(Namelable) // What should I do here to Get data from Lable?! 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("sp_Order", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", Name);

    }
}

Any Idea what to do?

Upvotes: 2

Views: 1276

Answers (2)

Md. Parvez Alam
Md. Parvez Alam

Reputation: 4596

I think you requirement is to find the label inside a datalist and then bind its value

so if assuem you doing in itemcommand

you find the control there

stirng str = ((label)e.item.findcontrol("lable1")).text.trim();

rest code goes here

Upvotes: 0

James
James

Reputation: 82136

Assuming your label is an asp:Label

cmd.Parameters.AddWithValue("@Name", YourLabel.Text);

If you are referring to pulling a label from a DataList you need to find the control first e.g.

var label = dataList.Items[i].FindControl("yourLabel") as Label;
cmd.Parameters.AddWithValue("@Name", label.Text);

FYI - it's generally not a good idea to couple your data access layer with your view...

Upvotes: 3

Related Questions