SAVE GRIDVIEW DATA AND NON GRIDVIEW DATA INTO DATABASE IN C# WITH IN THE FORM

Posted by qvg200286 under ASP.NET on 1/2/2013 | Points: 10 | Views : 19020 | Status : [Member] | Replies : 9
HI EXPERTS,

I HAVE GRID VIEW DESIGN WITH TEXT BOX CONTROLS,FILE UPLOAD CONTROLS AND DROP DOWN LISTS CONTROLS AND OUTSIDE THE GRID VIEW SOME CONTROLS ARE THERE SUCH AS TEXT BOXES,DROP DOWN LISTS .

BUT HOW CAN I INSERT INTO DATABASE GRID VIEW DATA AND NON GRID VIEW DATA IN SUBMIT BUTTON CLICK

PLEASE HELP ME ANY ONE ..............

MY DESIGN LIKE THIS

DDL1
DDL2
TXT1
TXT2
INSIDE THE GRID VIEW
GV1
DDL3 TXT3 TXT4 TXT5 TXT6 TXT7
TXT8 TXT8 TXT8 TXT9 TXT10

THANKS,
RAMEH




Responses

Posted by: Shubham on: 1/2/2013 [Member] Starter | Points: 25

Up
0
Down
hi if u using templet field in grid so you should only give grid textbox id on insert command it should work!!!!


Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: qvg200286 on: 1/3/2013 [Member] Starter | Points: 25

Up
0
Down
HI Shubham Garu,

can u give me simple example according to How To store Gridview data into database using c#,please help me i am new to gridview ..

thanks,
ramesh



qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: qvg200286 on: 1/4/2013 [Member] Starter | Points: 25

Up
0
Down
hi Shubham ,

thank u for your advice i have another problem i.e i have stored into data base but not insert the grid view controls values it show as nulls or zero in table column fields

where is the problem i am not understanding i have spend lot of time but i am not gettting the solution

this is my code:

protected void btnSubmit_Click1(object sender, EventArgs e)
{
DataTable dt = (DataTable)gvClient.DataSource;
dt.Columns.Add("ddlClient", typeof(string));
dt.Columns.Add("txtSun", typeof(string));
dt.Columns.Add("txtMon", typeof(string));
dt.Columns.Add("txtTue", typeof(string));
dt.Columns.Add("txtWed", typeof(string));
dt.Columns.Add("txtThu", typeof(string));
dt.Columns.Add("txtFri", typeof(string));
dt.Columns.Add("txtSat", typeof(string));
dt.Columns.Add("txttot", typeof(string));
foreach (GridViewRow row in gvClient.Rows)

{
DropDownList ddlClient = (DropDownList)row.FindControl("ddlClient");
TextBox txtSun = (TextBox)row.FindControl("txtSun");
TextBox txtMon = (TextBox)row.FindControl("txtMon");
//TextBox txtMon = (TextBox)gvrow.FindControl("txtSun");
TextBox txtTue = (TextBox)row.FindControl("txtTue");
TextBox txtWed = (TextBox)row.FindControl("txtWed");
TextBox txtThu = (TextBox)row.FindControl("txtThu");
TextBox txtFri = (TextBox)row.FindControl("txtFri");
TextBox txtSat = (TextBox)row.FindControl("txtSat");
TextBox txttot = (TextBox)row.FindControl("txttot");
DataRow dr = dt.NewRow();
dr["ddlClient"] = row.Cells[0].Text;
dr["txtSun"] = row.Cells[1].Text;
dr["txtMon"] = row.Cells[2].Text;
dr["txtTue"] = row.Cells[3].Text;
dr["txtWed"] = row.Cells[4].Text;
dr["txtThu"] = row.Cells[5].Text;
dr["txtFri"] = row.Cells[6].Text;
dr["txtSat"] = row.Cells[7].Text;
dr["txttot"] = row.Cells[8].Text;
//dr["fuBill1"] = row.Cells[].Text;
dr["ddlStatus"] = row.Cells[9].Text;
cmd = new SqlCommand("TimeSheetApplication008", con);--->timeshhetappliaction(procedure name)
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@txtSun",SqlDbType.NVarChar).Value=txtSun.Text;
cmd.Parameters.AddWithValue("@txtMon", SqlDbType.NVarChar).Value = txtMon.Text;
cmd.Parameters.AddWithValue("@txtTue", SqlDbType.NVarChar).Value = txtTue.Text;
cmd.Parameters.AddWithValue("@txtWed", SqlDbType.NVarChar).Value = txtWed.Text;
cmd.Parameters.AddWithValue("@txtThu", SqlDbType.NVarChar).Value = txtThu.Text;
cmd.Parameters.AddWithValue("@txtFri", SqlDbType.NVarChar).Value = txtFri.Text;
cmd.Parameters.AddWithValue("@txtSat", SqlDbType.NVarChar).Value = txtSat.Text;
cmd.Parameters.AddWithValue("@txttot", SqlDbType.NVarChar).Value = txttot.Text;
cmd.Parameters.AddWithValue("@ddlStatus", SqlDbType.NVarChar).Value = ddlStatus.SelectedValue;
//cmd.Parameters.AddWithValue("@TextBox7", TextBox7.Text);
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "alert('Records successfully saved!');", true);
}
}

THANKS,

RAMESH



qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/4/2013 [Member] Starter | Points: 25

Up
0
Down
Hi!!!! 200286

this code above u written is insert code right!!!!
show why r u use datatable simply use insert command to insert
and plzz i m not getting ur word correctly
if u r able to understand me some thing more so i think i will help u

i have stored into data base but not insert the grid view controls values it show as nulls or zero in table column fields
==============================================================================================================
in this line what r u saying??????

and what is ur database sql or mysql
because this problem only rise on mysql....

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: qvg200286 on: 1/4/2013 [Member] Starter | Points: 25

Up
0
Down
HI Shubham ,

I AM USING M.S SQL SERVER 2008 ...

OUTSIDE GRIDVIEW CONTROLS DATA INSERTED SUCCESFULLY BUT GRIDVIEW CONTROLS DATA NOT INSERTED CORRECTLY..

I RUN THE PROGRAM USING BREAKPOINT IN THE BELOW LINE txtSun.text show Empty String(during debugging)
cmd.Parameters.AddWithValue("@txtSun",SqlDbType.NVarChar).Value=txtSun.Text; -->

thanks
ramesh

qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/4/2013 [Member] Starter | Points: 25

Up
0
Down
hi !!!
i m not sure about that but are you try to run the code changing its datatype in proc.
because i think Nvarchar are not supported by vs...

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/4/2013 [Member] Starter | Points: 25

Up
0
Down
because due to string to Nvarchar giving u error

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: qvg200286 on: 1/4/2013 [Member] Starter | Points: 25

Up
0
Down
hi Shubham,

This Is MY DATA BASE [TABLE NAME] COLUMNS AND DATATYPES,...........

[ddlCompany] [int] NULL,
[ddlConsultant] [int] NULL,
[txtToDate] [date] NULL,
[txtFromDate] [date] NULL,
[ddlClient] [int] NULL,
[txtSun] [nvarchar](50) NULL,
[txtMon] [nvarchar](50) NULL,
[txtTue] [nvarchar](50) NULL,
[txtWed] [nvarchar](50) NULL,
[txtThu] [nvarchar](50) NULL,
[txtFri] [nvarchar](50) NULL,
[txtSat] [nvarchar](50) NULL,
[txttot] [nvarchar](50) NULL,
[ddlStatus] [int] NULL

SELECT *FROM [TABLE NAME]:

LIKE AS

ddlCompany ddlConsultant txtFromdate txtTodate ddlClient txtSun txtMon txtTue ...........................txttot
1 2 2013-01-12 2013-01-06 0 <EMPTY> <EMPTY> <Empty>.........................<EMPTY>

I AM INSERTING txtSun,txtMon,txtTue,..txttot Values like as 10,12,13,14...20 ..

MY PROBLEM IS THAT VALUES ARE NOT INSERTED INTO TABLE...(AFTER INSERTING ALL OF THE COLUMNS ARE EMPTY)..

I AM ALSO CHECK IN DEBUG MODE...
I RUN THE PROGRAM USING BREAKPOINT IN THE BELOW LINE txtSun.text show Empty String(during debugging)
cmd.Parameters.AddWithValue("@txtSun",SqlDbType.NVarChar).Value=txtSun.Text; -->

Thanks,
Ramesh








.




qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/9/2013 [Member] Starter | Points: 25

Up
0
Down
use
gridview.datasource=dt.table[];
gridvoew.databind();

after insert.

With Regards,
Bhupender Yadav

qvg200286, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response