I'm new to programming. I have a form (named personal_Info) that fills some person's personal data and saves it in the database table Personal_Information.
2nd I have another form that searches from Personal_Information and shows results in a grid view.
I now want: When search result show in data grid view and when I double click on any row of the shown results I want (personal_info) to open and make an edit at that form and save it.
Please help me.
here is little code which gets data from sql to grid
SqlConnection strconn = new SqlConnection("server=AAG-PC; Database=humanResource; Integrated Security=sspi");
strconn.Open();
SqlCommand strcmd = new SqlCommand("select * from Personal_Information where "
+ searchComboBx.SelectedItem
+ " like '%" + txtBxKeyword.Text.Trim() + "%'", strconn);
SqlDataAdapter ad = new SqlDataAdapter(strcmd);
DataSet ds = new DataSet ();
ad.Fill(ds);
strconn.Close();
gridViewSearchResult.DataSource = ds.Tables[0];
You can use dataadapter to update your changes. It will be something like
changes = ds.GetChanges();
if (changes != null)
{
adapter.Update(changes);
}
you can see more info here
Related
I have three dropdown list one for name, other month and other is year values. when I select name in dropdown list grid view display values of selected name. when I select month it has to display according to the month. when I select both name and month it has to display according to that. when I select all the three grid view has to shows the values for that condition.
Now am binding the data for dropdown list and grid view is in code behind file.
How to achieve that?
Guide me for ASPX and C# coding.
You have to create a dynamic function which accepts the parameters as you select the choices from the drop down.
public void BindGridView(string Name, string Month, string year)
{
//logic to fetch the data
}
Then you have to check the parameters are null or not. Based on these results you can put the queries to fetch the data.
I achieve that by using following coding.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sivConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT EMPNAME,CONVERT(VARCHAR(11),SALDATE,113),BAS_SAL,BASPAY,ADJ,DA,HRA,MA,TOT_SAL,EPF,FA,CATUP,NET_SAL from salentry where empname ='" + DropDownList1.SelectedValue + "' or DATEPART(MONTH,saldate)='" + DropDownList2.SelectedValue + "' or DATEPART(YEAR,saldate)='"+DropDownList3.SelectedValue+"'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();`
Do you have idea better than that?
I retrieve data from database like this:
OleDbDataAdapter dataAdapter
= new OleDbDataAdapter("SELECT * "
+ " FROM myTab1, myTab2"
+ " WHERE myTab1.col1 = myTab2.col3"
, connection);//OleDbConnection connection = new OleDbConnection();
DataTable dataTable = new DataTable("myDataTable");
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataAdapter.Fill(dataTable);
Then I use bindingSource in order to access the data in my program, everything works perfect. But after all changes I've made in bindingSource, I need so save them into the database. How can I do this?
Well normally you'd call Update() on the DataAdapter:
dataAdapter.Update(dataTable);
But since your query "joins" two tables it may not be so easy. It may be possible if you turn your SELECT query into a proper join:
"SELECT * "
+ "FROM myTab1 "
+ "JOIN myTab2 ON myTab1.col1 = myTab2.col3"
Some other alternatives:
Write an UPDATE statement that will put the right values in the right tables and set your dataAdapter's UpdateStatement
Populate the dataset with separate tables (one per base table) and join in your app
More info from MSDN:
http://msdn.microsoft.com/en-us/library/xzb1zw3x(v=vs.80).aspx
I am trying to populate some text boxes on a form with data pulled from MySQL into a data set.
I cant seem to get the code right and could use some help
string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection;
MySqlDataAdapter adapter;
DataTable DTItems;
connection = new MySqlConnection(ConnectionString);
try
{
//prepare query to get all records from items table
string query = "select * from spt_proposal where fr_Numer = "+ a+"";
//prepare adapter to run query
adapter = new MySqlDataAdapter(query, connection);
DataSet DS = new DataSet();
//get query results in dataset
adapter.Fill(DS);
textBox5.Text = DS.Tables[0].Rows[0].ToString();
This does not hit the text box at all.
a in the query is a variable that is pulled from a different form
and the query should pull 32 different things from the database in a row
There are about 9 text boxes on the form that i will have to fill with different data from this row.
Anyone have a better way to do this?
Brent
Try:
textBox5.Text = DS.Tables[0].Rows[0][0].ToString();
Remember, that a Table is pretty much a two dimensional array. Your code literally gets to the 1st row in the DataSet, but doesn't take into account the Column.
Below is my Datagridview code to get the data from employee table.
the problem am facing is ,my employee table have 10 columns (ID,emplNo,Dob,JoingData...etc)
i just want to fill my grid with only ID,EmplyNo and DOB.
but the below code get everything,please advise me what i suppose to do to get only particular column
string sql = "select * from Employee";
SqlConnection connection = new SqlConnection(CONNECTION_STRING);
//SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
dataadapter = new SqlDataAdapter(sql, connection);
// DataSet ds = new DataSet();
ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, scrollVal, 5, "Employee");
connection.Close();
dgMessages.DataSource = ds;
dgMessages.DataMember = "tEmployee";
instead of
string sql = "select * from Employee";
do
string sql = "select ID, EmplyNo, DOB from Employee";
either change your select to only get the supset of what you want or use the designer:
it's a bit of a pain because you first have to add an unbound column and then edit the just added column to setthe DataPropertyName to a column-name in your resulting table - but it works.
You find this dialogs by clicking the "..." in the "columns" property of the PropertyEditor for the DataGridView or by clicking the little "Play"-Button on the top-right of the Grid in the designer (when it is selected)
Almost forgot: IMPORTANT: you need to add EVERY column and set Visible=False on those you don't want to see - I think this is different in WebForms where there is something like AutogenerateColumns.
I have a program written in c# visual studio 2008 with SQL server 2005 (.mdf) database.
Here is part of the code:
...
SqlCommandBuilder cb;
cb = new SqlCommandBuilder(dataAdapter);
String[] dataList =new String [8];
...
DataTable resultTable = new DataTable();
FillDataList(data);
dataAdapter = new SqlDataAdapter("insert into ProcessData values('" + dataList[0] + "','" + dataList[1] + "','" + dataList[2] + "','" + dataList[3] + "','" + dataList[4] + "','" + dataList[5] + "','" + dataList[6] + "','" + dataList[7]+"')", con);
dataAdapter.Fill(resultTable);
...
My questions are:
1) Where is the data I added in those lines stored?
2) Why is it that when I Right-Click with the mouse in the Server Explorer->Data Connections->Tables->ProcessData (my table's name)->"show Table Data", I don't see the data but just NULL in the columns, and how can i see the data there?
3) Why when I present this data in a DataGridView sometimes it shows the data and sometimes it doesn't?
Many Thanks!
Without seeing more info on the datalist object and its construction, it's impossible to say. However, you're using the Fill and SQLDataAdapter incorrectly. The FILL relies on the SQLDataAdapter having a SelectCommand property set, which is what your code is bunging the INSERT statement into (that's what the SQLDataAdapter's constructor does). So...your Fill returns nothing as there's no SELECT where there should be.
Your INSERT should be part of the dataadapter's InsertCommand, and you'll need to write a separate SELECT statement to get anything into your resultTable.
I think you need to read some basic documentation on ADO.NET before you write any more code. You're passing an INSERT statement to the SqlDataAdapter constructor, which takes a SELECT statement. You're using the Fill method of SqlDataAdapter where you should be using the Update method. You're building a SQL string with hard-coded values in it where you should be using SqlParameters with references to the DataTable Columns. And is the fact that your DataTable is called "data" but that SQL string is using indexed properties from "dataList" just a typo? Because if it is, DataTable doesn't have an indexed property.
Try doing it like this (you need to fill in the correct connection string though):
// Get the db connection
SqlConnection dbCon = new SqlConnection("connection string");
// Select the data from the database table into a DataSet (even if it's empty)
DataSet myData = new DataSet();
SqlDataAdapter dbAdapter = new SqlDataAdapter("select * from ProcessData", dbCon);
dbAdapter.Fill(myData);
myData.Tables[0].TableName = "ProcessData"; // Keeps the table name consistent for the DataMember property
// Use the command builder to add insert, delete, update commands to your adapter
// You must have a primary key on the table for these to work, though
SqlCommandBuilder dbComBuilder = new SqlCommandBuilder(dbAdapter);
dbAdapter.InsertCommand = dbComBuilder.GetInsertCommand();
dbAdapter.DeleteCommand = dbComBuilder.GetDeleteCommand();
dbAdapter.UpdateCommand = dbComBuilder.GetUpdateCommand();
// Bind the data set to the GridView for viewing / editing
yourGridControl.AutoGenerateColumns = true; // Optional, if you haven't manually added the columns
yourGridControl.DataSource = myData;
yourGridControl.DataMember = "ProcessData";
// Use the db adapter to update the database (by calling those commands) with
// changes made to the DataSet through the grid. This would go in a different
// form event, like a Save Button click.
dbAdapter.Update();
If you don't have a primary key in the ProcessData table, you can directly insert values using this command:
// Insert the data directly with a command
SqlCommand dbInsCommand = new SqlCommand("insert into ProcessData values (" + val1 + "," + val2 + ")", dbCon);
dbInsCommand.ExecuteNonQuery();