Hello Guys,
I have been trying to solve the following problem from past 3 days with no luck.
This is what I am trying to do:
I want to allow user (Professor) to choose a course material using "asp:FileUpload " control, upload into the database by clicking "asp:Button " control and allow Professor to view the files he/she uploaded using "asp:Repeat er" control.
The procedure is working perfectly fine. But when I place the "asp:Repeat er" control in Ajax UpdatePanel, I am unable to retrieve file from "asp:FileUpload " control.
Please help me out. Thanks a million in advance.
1. The following is "CreateSchedule .aspx" page, using this page a professor can upload course materials.
2. "CreateSchedule .aspx.cs" page:
I have been trying to solve the following problem from past 3 days with no luck.
This is what I am trying to do:
I want to allow user (Professor) to choose a course material using "asp:FileUpload " control, upload into the database by clicking "asp:Button " control and allow Professor to view the files he/she uploaded using "asp:Repeat er" control.
The procedure is working perfectly fine. But when I place the "asp:Repeat er" control in Ajax UpdatePanel, I am unable to retrieve file from "asp:FileUpload " control.
Please help me out. Thanks a million in advance.
1. The following is "CreateSchedule .aspx" page, using this page a professor can upload course materials.
Code:
<asp: Content ID="Content1" ContentPlaceHolderID="ContentPlaceholder1" Runat="Server">
<table>
<tr>
<td>Select File:</td>
<td><asp:FileUpload ID="CMFileUpload" runat="server" /></td>
</tr>
<tr>
<td colspan=2>
<asp:Button ID="CMUploadButton" runtat="server" Text="Upload" onClick="CMUploadButton_Click">
</td>
</tr>
<td>Course Materials Uploaded:</td>
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:Repeater ID="CMRepeater" runat="server"><ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Click" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"File_UID") %>'><%#DataBinder.Eval(Container.DataItem,"File_Name2") %></asp:LinkButton>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/wrong-icon.gif" OnCommand="ImageButton1_Click" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"File_UID") %>' />
</ItemTemplate></asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CMUploadButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</td>
<tr>
</tr>
</table>
</asp:Content>
Code:
protected void CMUploadButton_Click(object sender, EventArgs e)
{
if (!CMFileUpload.HasFile) return;
int weekNo = 1;
byte[] binary = new byte[CMFileUpload.PostedFile.ContentLength];
binary = CMFileUpload.FileBytes;
SqlParameter param = null;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=CSMS;Integrated Security=true";
SqlCommand cmd = new SqlCommand("UploadCourseMaterials", conn);
cmd.CommandType = CommandType.StoredProcedure;
param = new SqlParameter("@File_Name1", SqlDbType.NVarChar, 50,
ParameterDirection.Input, false, 0, 0, "",
DataRowVersion.Default, CMFileUpload.FileName);
cmd.Parameters.Add(param);
param = new SqlParameter("@File_Name2", SqlDbType.NVarChar, 50,
ParameterDirection.Input, false, 0, 0, "",
DataRowVersion.Default, CMNameTextBox.Text);
cmd.Parameters.Add(param);
param = new SqlParameter("@File_Content", SqlDbType.Image);
param.Direction = ParameterDirection.Input;
param.Value = binary;
cmd.Parameters.Add(param);
param = new SqlParameter("@Content_Type", SqlDbType.NVarChar, 50,
ParameterDirection.Input, false, 0, 0, "",
DataRowVersion.Default, CMFileUpload.PostedFile.ContentType);
cmd.Parameters.Add(param);
param = new SqlParameter("@WeekID", SqlDbType.Int, 4);
param.Value = weekNo;
cmd.Parameters.Add(param);
conn.Open();
cmd.ExecuteNonQuery();
DisplayCourseMaterials();
}
private void DisplayCourseMaterials()
{
SqlConnection mySQLconnection = new SqlConnection();
mySQLconnection.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=CSMS;Integrated Security=true";
if (mySQLconnection.State == ConnectionState.Closed)
{
mySQLconnection.Open();
}
SqlCommand mySqlSelect = new SqlCommand("select * from CourseMaterials", mySQLconnection);
mySqlSelect.CommandType = CommandType.Text;
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
CMRepeater.DataSource = myDataSet;
CMRepeater.DataBind();
if (mySQLconnection.State == ConnectionState.Open)
{
mySQLconnection.Close();
}
}
Comment