Click here to Skip to main content
15,916,215 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my output one column in dynamically created, that column name is column1(5) in the bellow output.i want to set that column name.

What I have tried:

This is markup code
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"  Visible="true">
           
        </asp:GridView>

this is my code
C#
protected void Button1_Click(object sender, EventArgs e)
       {
           SqlCommand cmd = new SqlCommand(" select S1_DateTime,S1_Weight,S2_Weight,AdjustmentWeight,ABS(convert(float,S1_Weight)-convert(float,S2_Weight)-convert(float,AdjustmentWeight)) from dbo_OrderLines1 where convert(varchar(25),S1_DateTime,126) like '%" + Calendar1.SelectedDate.ToString("yyyy-MM-dd") + "%'", con1);

           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           Label1.Text ="number of rows = "+ dt.Rows.Count.ToString();
           TextBox1.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
           GridView1.Visible = true;
           GridView1.DataSource = dt;
           GridView1.DataBind();

       }

this is ouptput coming like this columns of the output table 1.S1_DateTime 2.S1_Weight 3.S2_Weight 4.AdjustmentWeight 5.Column1,i need to set the column1(5) name how to do?
Posted
Updated 27-May-16 1:06am
Comments

1 solution

You can set the column name by giving it an alias within the query:
SQL
... ABS(...) As YourColumnName ...


Rather than converting your datetime column to a string, test whether it is greater than or equal to the selected date, and less than one day after the selected date.
SQL
S1_DateTime >= @SelectedDate And S1_DateTime < DateAdd(day, 1, @SelectedDate)


Wrap your SqlCommand and SqlConnection objects in using statements.

And use parameterized queries to avoid SQL Injection[^].

C#
using (SqlConnection con = new SqlConnection("..."))
using (SqlCommand cmd = new SqlCommand(" select S1_DateTime, S1_Weight, S2_Weight, AdjustmentWeight, ABS(convert(float, S1_Weight) - convert(float, S2_Weight) - convert(float, AdjustmentWeight)) As YourColumnName from dbo_OrderLines1 where S1_DateTime >= @SelectedDate And S1_DateTime < DateAdd(day, 1, @SelectedDate)", con))
{
    cmd.Parameters.AddWithValue("@SelectedDate", Calendar1.SelectedDate);
    
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    
    Label1.Text ="number of rows = "+ dt.Rows.Count.ToString();
    TextBox1.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
    GridView1.Visible = true;
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
 
Share this answer
 
Comments
Nagarjuna99 27-May-16 7:10am    
thanks @richard deeming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900