Thursday, March 1, 2012

Article 3: using ado.net connector on sharepoint


reate new database: SQLConnectorSharepoint
Create new Table in database SQLConnectorSharepoint with name is Products
Create new columns: ID, Name, Price
Enter some data:
You can download database and project then restore it into your PC
Open Visual studio 2010 | New Project | Empty Sharepoint Project | Name is DisplaySQLTableToSP
Deploy as a farm solution
Right click Project | Add new item | Visual Web Part | Name is DisPlaySQLTableToSP
User Interface (UI) follows as:

Copy code here to below
<asp:LiteralID="literalID"runat="server"></asp:Literal>
<asp:LiteralID="literalName"runat="server"></asp:Literal>
<asp:LiteralID="literalPrice"runat="server"></asp:Literal>
Now, turn on view code to develop
Code in event page_load
Using
usingSystem.Data.SqlClient;
usingSystem.Data;

Paste this code to event Page_Load
try
            {
stringconnectionString = "Data Source=.;Initial Catalog=SQLConnectorSharepoint ;Integrated Security=True";
string query = "SELECT ID, Name, Price FROM Products WHERE ID=(SELECT MAX(ID) from Products)";
SqlDataAdapter da = newSqlDataAdapter(query, connectionString);
DataTabletableProducts = newDataTable();
da.Fill(tableProducts);
if (tableProducts.Rows.Count>0)
                {
literalID.Text = tableProducts.Rows[0]["ID"].ToString();
literalName.Text = tableProducts.Rows[0]["Name"].ToString();
literalPrice.Text = Convert.ToString(tableProducts.Rows[0]["Price"]);
                }

            }
catch (Exception ex)
            {
Response.Write("Exception: " + ex.Message);
            }
        }
Open your sharepoint and add webpart, you will see result follows as:
Add caption

1 comment:

  1. It's in point of fact a nice and useful piece of information. I'm satisfied that you
    just shared this useful information with us. Please keep us informed
    like this. Thanks for sharing.
    My website :: seen on tv slimmer shaper

    ReplyDelete