0

I am getting following thing when I trying to get data from Sql server data base and display it in GridView. My code as follows:

string connstr = @"Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123";
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        DataTable dt = new DataTable();

        SqlConnection conn = new SqlConnection(connstr);
        conn.Open();
        da = null;
        string strsql = "Select File_ID,File_Name,File_Type from Upload_file";
        da = new SqlDataAdapter(strsql, conn);
        da.Fill(ds, "Upload_file");

        GridView1.DataSource = ds.Tables["Upload_file"].DefaultView;
        GridView1.DataBind();   

enter image description here

1
  • It's not really clear what you want the output to be. Don't make us guess. Commented Mar 4, 2014 at 6:27

4 Answers 4

1

To display data in grid view on your view do something like this (ASP.Net MVC razor):

<table>
<tr>
<td>
Heading
</td>
</tr>

@foreach(var item in Model)
{
<tr>
<td>
@* Something that you want to display *@
</td>
</tr>
}

</table>
Sign up to request clarification or add additional context in comments.

Comments

0

If you want do particular operation to data retrieved you should do it in the rowdatabinding/celldatabinding event, otherwise you should define columns or in the markup or by codebehind http://msdn.microsoft.com/en-us/library/aa479342.aspx

Comments

0

Try this

           string connstr = @"Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123";
           SqlConnection conn = new SqlConnection(connstr);
           conn.Open();
           SqlDataAdapter adapter = new SqlDataAdapter("Select File_ID,File_Name,File_Type from Upload_file ", conn);
           DataTable dt = new DataTable();
           adapter.Fill(dt);
           conn.Close();
           GridView1.DataSource = dt;

           GridView1.DataBind();

Hope this will help

Comments

0

The simplest way could be:

//Create sql DATA source by passing connection string and select command
SqlDataSource mySqlDataSource= new SqlDataSource("Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123", "Select File_ID,File_Name,File_Type from Upload_file");

//Assign the data source to GridView data source
GridView1.DataSource = mySqlDataSource;
//Do data binding
GridView1.DataBind();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.