Hi micah,
I have created sample code which full-fill your requirement.So please refer the code carefully.
HTML
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Enter Title:
<asp:TextBox ID="txtTitle" Placeholder="Enter your Title " runat="server" />
</td>
<td>
EnterURL:
<asp:TextBox ID="txtUrl" Placeholder="Enter your URL " runat="server" />
</td>
</tr>
<tr>
<td>
Image:
<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlCategory" runat="server">
<asp:ListItem Value="0" Text="Select" />
<asp:ListItem Value="buy" Text="buy Now" />
<asp:ListItem Value="Read" Text="Read Now" />
<asp:ListItem Value="Register" Text="Register Now" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAdd" OnClick="OnAdd" Text="Add" runat="server" />
</td>
</tr>
</table>
</div>
<br />
<br />
<asp:datalist id="DlAdverties" runat="server" xmlns:asp="#unknown">
<headertemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td>
<asp:Image ImageUrl='<%#Eval("Image") %>' runat="server" />
</td>
</tr>
<tr>
<td>
<asp:hyperlink id="HyperLink1" runat="server" navigateurl='<%# Eval("URL") %>' text='<%# Eval("Category") %>' />
</td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:datalist>
</form>
C#
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("SELECT * FROM WebLink", con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
DlAdverties.DataSource = dt;
DlAdverties.DataBind();
}
}
protected void OnAdd(object sender, EventArgs e)
{
string imagename = FileUpload1.PostedFile.FileName;
FileUpload1.SaveAs(Server.MapPath("Image/" + imagename));
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("INSERT INTO WebLink values(@Title,@URL,@Image,@Category)", con);
cmd.Parameters.AddWithValue("@Title", txtTitle.Text.Trim());
cmd.Parameters.AddWithValue("@URL", txtUrl.Text.Trim());
cmd.Parameters.AddWithValue("@Image", "Image/" + imagename);
cmd.Parameters.AddWithValue("@Category", ddlCategory.SelectedValue.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
this.GetData();
}
SQL
CREATE TABLE WebLink(
Id INT IDENTITY PRIMARY KEY NOT NULL
,Title VARCHAR(100)
,URL VARCHAR(100)
,[Image] NVARCHAR(50)
,Category VARCHAR(50)
);
Screenshot