none
how to insert image path RRS feed

  • Question

  •  

    hi to all

    I  am creating travels project using  C#,  .net2005 and sqlserver 2000,

    I got strucked i at paticular part that i want to insert image path in the database and i want to display in the datagridview

    please solve my problem its very urgent, my boss kept knife on my throat...

    thank you

    raghu

    Saturday, December 15, 2007 10:52 AM

Answers

  • Well,

     

    What I understood from the above question is that: How to insert image path in a sql server database?

     

    It is just another data entry. For this you need 2/3 things todo:

     

    1. Create a table (myTABLE)with columns number<id>, varchar<imagepath>. in the desired database.

    2. Create a query

    string strQuery = "INSERT INTO myTABLE (imagepath) VALUES('c:\test1.jpg')";

    3. Do following:

     

    OleDbCommand dbCommand = new OleDbCommand(strQuery, new OleDbConnection('C:\yourDatabase.MDB');

    int nRecords = dbCommand.ExecuteQuery();

     

    Hope this helps.
    Saturday, December 15, 2007 11:23 AM
  • In order to display the image according to the path column, you can add a DataGridViewImageColumn to the DataGridView, and then assign a proper image to each cell after the data has been bound. This approach is demostrated below:

        private void table1DataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

        {

            DataGridView grid = table1DataGridView;

            for (int i = 0; i < grid.Rows.Count; i++)

            {

                if (grid.RowsIdea.IsNewRow)

                    continue;

     

                //This is the cell that contains the path of the image.

                DataGridViewTextBoxCell pathCell = (DataGridViewTextBoxCell)table1DataGridView[2, i];

     

                string path = (string)pathCell.Value;

                Image img = Image.FromFile(path);

     

                //This is the cell that displays the image.

                DataGridViewImageCell photoCell = (DataGridViewImageCell)grid[3, i];

                photoCell.Value = Properties.Resources.fish;

            }

        }

     

    Best Regards

    Chunsheng Tang

    Tuesday, December 18, 2007 4:51 AM

All replies

  • Raghu,

     

    Can u give us some details regarding ur problem? Like, do u want to display the path in the datagrid or the image itself? What kind of object u make use to store image in the db? Let me know.


    Thanks,

    Reg,
    Venkat

     

    Saturday, December 15, 2007 11:20 AM
  • Well,

     

    What I understood from the above question is that: How to insert image path in a sql server database?

     

    It is just another data entry. For this you need 2/3 things todo:

     

    1. Create a table (myTABLE)with columns number<id>, varchar<imagepath>. in the desired database.

    2. Create a query

    string strQuery = "INSERT INTO myTABLE (imagepath) VALUES('c:\test1.jpg')";

    3. Do following:

     

    OleDbCommand dbCommand = new OleDbCommand(strQuery, new OleDbConnection('C:\yourDatabase.MDB');

    int nRecords = dbCommand.ExecuteQuery();

     

    Hope this helps.
    Saturday, December 15, 2007 11:23 AM
  •  

    hi venkat thanks for your reply,

    I want to sore image path in sqlserver and i want to display image in the datagridview.

    i think now you can understand my question, plese solve my problem its very urgent

    thank you

    raghu

    Monday, December 17, 2007 6:18 AM
  • thanks solar

    after doing your codding how can i display image in the datagridview

     

    Monday, December 17, 2007 11:11 AM
  • In order to display the image according to the path column, you can add a DataGridViewImageColumn to the DataGridView, and then assign a proper image to each cell after the data has been bound. This approach is demostrated below:

        private void table1DataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

        {

            DataGridView grid = table1DataGridView;

            for (int i = 0; i < grid.Rows.Count; i++)

            {

                if (grid.RowsIdea.IsNewRow)

                    continue;

     

                //This is the cell that contains the path of the image.

                DataGridViewTextBoxCell pathCell = (DataGridViewTextBoxCell)table1DataGridView[2, i];

     

                string path = (string)pathCell.Value;

                Image img = Image.FromFile(path);

     

                //This is the cell that displays the image.

                DataGridViewImageCell photoCell = (DataGridViewImageCell)grid[3, i];

                photoCell.Value = Properties.Resources.fish;

            }

        }

     

    Best Regards

    Chunsheng Tang

    Tuesday, December 18, 2007 4:51 AM
  • You can also use inline code in template field:

    Code Block

      <asp:GridView ID="dataGrid" runat="server" Width="90%" BackColor="LemonChiffon" AutoGenerateColumns="False"/>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href='<%# Eval("Path")%>'>
                            <img src="<%# Eval("Image") %>" style="border-style: none" alt="Click to download"></a>
                    </ItemTemplate>
                </asp:TemplateField>
               
                <asp:HyperLinkField DataNavigateUrlFields="EditUrl" DataTextField="Title" HeaderText="Document" />
                <asp:BoundField DataField="Regnumber" HeaderText="Reg no" />
            </Columns>
      </asp:GridView>

    Tuesday, December 18, 2007 6:33 AM