PDA

View Full Version : Editable Table , using MYSQL, got a better way to do it then me?


hamsterchaos
05-15-2008, 05:13 PM
I have spent 3 months and have a good functioning .net applicaiton, where a table is populated from the mysql database, and you can insert and update rows.

It uses:

MYSQL
Gridview component
ODBC connection


Unfortuantely this requires ALOT of manual coding, including, stating the parameters to be used for each of the insert, update, select statements ( you cant use Visual Studios GUI because Im connecting to a MYSQL database).

It also involves recoding all of the row updating methods, insert methods , and on top of that use complex "find" statements to figure out which text box to gather the input from.

I include examples below:



<EditItemTemplate>
<asp:TextBox ID="finishTXT" runat="server" Text='<%# Eval("finish") %>' />
</EditItemTemplate></asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="right" HeaderText="Length">
<ItemTemplate ><asp:literal ID="length" runat="server" Text='<%# formatstart(Eval("length")) %>' /></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lengthTXT" runat="server" Text='<%# Eval("length") %>' />
</EditItemTemplate></asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="right" HeaderText="Room">
<ItemTemplate ><asp:literal ID="room" runat="server" Text='<%# Eval("room") %>' /></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="roomTXT" runat="server" Text='<%# Eval("room") %>' />
</EditItemTemplate></asp:TemplateField>

...


Name="amendmentMade" />
<asp:Parameter Name="amendment" />
<asp:Parameter Name="course split" />
<asp:Parameter Name="group" />
<asp:Parameter Name="class" />
<asp:Parameter Name="banner code" />
<asp:Parameter Name="person code" />
<asp:Parameter Name="personcodeleadteacher" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="day" />
<asp:Parameter Name="dates" />
<asp:Parameter Name="start" />
<asp:Parameter Name="finish" />
...

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
Session["boobs"] = e.Row.RowIndex; ;


}
}


.....


public void this_rowUpdating(Object sender, GridViewUpdateEventArgs e)
{
int selectedrow = Convert.ToInt32(Session["boobs"]);


Box groupTXT = (TextBox)GridView1.Rows[selectedrow].Cells[4].FindControl("TXTgroup");
TextBox classTXT = (TextBox)GridView1.Rows[selectedrow].Cells[4].FindControl("TXTclass");
TextBox bannercodeTXT = (TextBox)GridView1.Rows[selectedrow].Cells[4].FindControl("TXTbannercode");
TextBox personcodeTXT = (TextBox)GridView1.Rows[selectedrow].Cells[4].FindControl("TXTpersoncode");
TextBox personcodeleadteacherTXT = (TextBox)GridView1.Rows[selectedrow].Cells[4].FindControl("TXTpersoncodeleadteacher");



e.NewValues["day"] = dayTXT.Text;
e.NewValues["dates"] = datesTXT.Text;
e.NewValues["start"] = startTXT.Text;
e.NewValues["finish"] = finishTXT.Text;



....


public void gridChange(Object sender, GridViewCommandEventArgs e)
{



if (e.CommandName == "Insert")
{

if (Page.IsValid)
{
TextBox txtGroup = (TextBox)GridView1.FooterRow.FindControl("txtGroup");
TextBox txtBannerCode = (TextBox)GridView1.FooterRow.FindControl("txtBannerCode");

if (Course.Text != null)
{
SqlDataSource1.InsertParameters["Course"].DefaultValue = Course.Text;
SqlDataSource1.InsertParameters["ID"].DefaultValue = txtId.Text;
SqlDataSource1.InsertParameters["Day"].DefaultValue = txtDay.Text;

....


These are all examples of manual hand written code required for one fully funciontal editable gridview in c# with mysql.

Am I being retarded? Is there a much easier way to do this?

Thanks for your time, forgive the bad spelling.

chump2877
05-16-2008, 12:39 AM
Well here's my two cents....

Since you are using a SqlDataSource control, then I assume your connection string appears in the web.config of your application. Are you encrypting the connection string? You should. As long as your data access code is physically in the root directory of the application, that should be a requirement -- for security reasons obviously.

I avoid using the SqlDataSource control, and manually including the connection string in the web.config, by creating a separate, reusable DataAccess class library project and referencing the project in my web application. That way all of your data access credentials are safe and secure -- locked away inside of a .dll file.

And regarding your use of MySQL: Such a DataAccess class also doesn't distinguish between databases. The same class can be used for SQL Server, MySQL, etc. -- the only thing that changes in the class is the connection string.

As far as using FindControl a lot: It looks a little messy, but I know I've done the same thing to find a control inside of a GridView. I don't think your retarded for doing it like that. ;)