abc abc - 3 months ago 16
C# Question

how to display the error messages in gridview on the web page

output on the webpage-

Choose File-->file uploader ReadFile(button) Insert Records(button)



Name Address Dob Phone EmailId LoginName Password
bhaya Vizag 22/01/90
hyd 03/01/80 8526789543 sandya@gmail.com saritha navya hyd 9976543214 navya@gmail.com
geeta hyd 04/07/96 8883458765 geetha@gmail.com geetha

Row Number1 EMAIL ID Field Missing
Row Number2 Name Field Missing
Row Number2 BATCH NO Field Missing
Row Number3 DOB Field Missing
Row Number3 EMPLOYEE ID Field Missing


my output is like this on the webpage but i need these error messages to be placed in gridview how can i do this below is my code

using the below code i am able to display error messages on the webpage but i want to place those messages in grid view how can i do this can anybody help me out with this.

string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd;
string msg = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
UploadStatusLabel.Text = "";
}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
string fileName = SaveToAppFolder(FileUpload1.PostedFile);
OdsReaderWriter obj = new OdsReaderWriter();
DataSet dset = obj.ReadOdsFile(fileName);
DataTable dt = dset.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
for (int i = 1; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
string Text = dt.Rows[i][j].ToString();
if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{

}
else
{
if (j == 0)
{
msg += "Row Number" + i + " " + " " + "Name Field Missing" + "<br>";
}
else if (j == 2)
{
msg += "Row Number" + i + " " + " " + "DOB Field Missing" + "<br>";
}
else if (j == 4)
{
msg += "Row Number" + i + " " + " " + "EMAIL ID Field Missing" + "<br>";
}
else if (j == 7)
{
msg += "Row Number" + i + " " + " " + "EMPLOYEE ID Field Missing" + "<br>";
}
else if (j == 8)
{
msg += "Row Number" + i + " " + " " + "BATCH NO Field Missing" + "<br>";
}
}
}
}

Session["Message"] = msg;
}
catch (Exception ex)
{
UploadStatusLabel.Text = "Only .ODS Files Are Allowed";
}
GridView1.Visible = true;

}
string SaveToAppFolder(HttpPostedFile file)
{
string savePath = Server.MapPath(".") + "\\TempFiles\\";
string fileName = FileUpload1.FileName;
string pathToCheck = savePath + fileName;
string tempfileName = "";
if (System.IO.File.Exists(pathToCheck))
{
int counter = 2;
while (System.IO.File.Exists(pathToCheck))
{
tempfileName = counter.ToString() + fileName;
pathToCheck = savePath + tempfileName;
counter++;
}

fileName = tempfileName;
}
else
{

}
savePath += fileName;
FileUpload1.SaveAs(savePath);
return savePath;
}

protected void Button2_Click(object sender, EventArgs e)
{
GridView1.Visible = false;
}

protected void btn_insert_Click(object sender, EventArgs e)
{

string msg1 = (string)Session["Message"];
foreach (GridViewRow g1 in GridView1.Rows)
{
SqlConnection con = new SqlConnection(connStr);
cmd = new SqlCommand("insert into Details1(Name,Address,Dob,Phone,EmailId,LoginName,Password,EmployeeId,BatchNo,Active) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
UploadStatusLabel.Text = "Records Inserted Successfully";

if (string.IsNullOrEmpty(msg1))
{
}
else
{
UploadStatusLabel.Text = msg1;
}
}

protected void BtnSave_Click(object sender, EventArgs e)
{
//Response.ClearContent();
//Response.AddHeader("content-disposition", "attachment; filename=Details.ods");
//Response.ContentType = "vnd.oasis.opendocument.text";
//System.IO.StringWriter sw = new System.IO.StringWriter();
//HtmlTextWriter htw = new HtmlTextWriter(sw);
//GridView1.RenderControl(htw);
//Response.Write(sw.ToString());
//Response.End();
string msg1 = (string)Session["Message"];
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=Details.ods");
Response.ContentType = "vnd.oasis.opendocument.text";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
//... Code to write messages ...
StringBuilder objSb = new StringBuilder();
objSb.Append("Messages");
objSb.Append("");
string szText = objSb.ToString();
foreach (var szMessage in msg1)
{
szText = szText + szMessage + "";
}
Response.Write(szText.ToString());
//...
Response.End();


}
public override void VerifyRenderingInServerForm(Control control)
{

}


code in aspx page

<div>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="ReadFile" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Refresh" OnClick="Button2_Click" />
<asp:Button ID="BtnSave" runat="server" Text="Save" onclick="BtnSave_Click" />
<asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click" Text="Insert Records" />
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeader="False" Height="114px" Width="182px">
</asp:GridView>
<asp:Label ID="UploadStatusLabel" runat="server"></asp:Label>
</div>

</div>

Answer

Please change this line of code:

szText = szText + szMessage + "\r<br>";

To this:

szText += szMessage;
Comments