Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i have Textboxs and a save button

string FemaleCondoms = txtFemaleCondoms.Text;
               string MaleCondoms = txtMaleCondoms.Text;
               string Lube = txtLube.Text;
               string HTS = txtHTS.Text;
               string IEC = txtIEC.Text;

<asp:Button ID="btnSave" runat="server" Text="Save Data" OnClick ="SaveData_Click"  />


When ever i click on save data , info must be sent to DB and Retrieve that info on textboxes. without loading

What I have tried:

//using (SqlConnection con = new SqlConnection(Initialize.ConnectString))
               //{
               //    if (con.State == System.Data.ConnectionState.Closed)
               //    {
               //        con.Open();
               //    }

               //    using (SqlCommand cmd = new SqlCommand("Select FemaleCondoms from OutreachTotalCommodityDistributedPerOutreach Where FemaleCondoms=@FemaleCondoms", con))
               //    {


               //        cmd.Parameters.AddWithValue("@FemaleCondoms", FemaleCondoms);
               //        cmd.Parameters.AddWithValue("@MaleCondoms", MaleCondoms);

               //        cmd.Parameters.AddWithValue("@Lube", Lube);

               //        cmd.Parameters.AddWithValue("@IEC", IEC);
               //        cmd.Parameters.AddWithValue("@HTSKits", HTS);
               //        using (SqlDataReader rd = cmd.ExecuteReader())
               //        {
               //            if(rd.Read() )
               //            {
               //                txtFemaleCondoms.Text = rd["FemaleCondoms"].ToString();                          }
               //        }
               //    }
Posted
Updated 6-Jul-18 18:38pm
Comments
ZurdoDev 6-Jul-18 9:47am    
Where are you stuck? You look like you are spamming.

1 solution

There are tons of examples that shows how to perform CRUD using ADO.NET. You just have to find them at google.

As a reference, here's a quick example:

To Insert, you can do:

C#
protected void InsertData(){
    using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
        string sql = "INSERT INTO YourTableName (Field1,Field2,Field3) VALUES (@Param1,@Param2,@Param3)";
        using(SqlCommand cmd = new SqlCommand(sql,connection)){
               cmd.Parameters.AddWithValue("@Param1", TextBox1.Text)
               cmd.Parameters.AddWithValue("@Param2", TextBox2.Text)
               cmd.Parameters.AddWithValue("@Param3", TextBox2.Text)
               cmd.CommandType = CommandType.Text
               cmd.ExecuteNonQuery()
        }
    }
}


To Select, you can do:

C#
protected void FetchData(string searchText){
    using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
        string sql = "SELECT Field1, Field2, Field3 FROM TableName WHERE YourFieldName = @Param1";
        using(SqlCommand cmd = new SqlCommand(sql,connection)){
                cmd.Parameters.AddWithValue("@Param1", searchText);

                DataTable dt = new DataTable();
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                ad.Fill(dt);

                //check if the query returns any data
                if (dt.Rows.Count > 0) { 
                       TextBox1.Text = dt.Rows[0]["Field1"].ToString();
                       TextBox2.Text = dt.Rows[0]["Field2"].ToString();
                       TextBox3.Text = dt.Rows[0]["Field3"].ToString();
                }
                else
                {
                     //No records found
                }
        }
    }
}
 
Share this answer
 
Comments
Member 13901256 9-Jul-18 8:31am    
Thankx, this Worked but th thing is , once i have inserted values and click on the save button, it only returns the saved data when i have click the button twice. it doesnt automatically reload..
Vincent Maverick Durano 9-Jul-18 8:49am    
You mean it only saves on second click of the Button?
Member 13901256 9-Jul-18 9:08am    
it save the first time but on display those values the second time.
Vincent Maverick Durano 9-Jul-18 9:36am    
update your post and provide the current code that you have. It's hard to guess without codes. Also have you tried debugging your code, set a break point and stepping into it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900